--- kadu/kadu-core/misc.h 2007-03-26 00:51:02.000000000 +0200 +++ kadu/kadu-core/misc.h 2007-03-26 00:57:34.000000000 +0200 @@ -9,6 +9,8 @@ #include #include +#include + class QComboBox; class QLineEdit; class QMenuData; @@ -215,4 +217,8 @@ extern const char *detailed_version; +// zoptymalizowane i bezpieczniejsze odpowiedniki funkcji strncat oraz strncpy (libgadu/compat) +size_t strlcat(char *dst, const char *src, size_t size); +size_t strlcpy(char *dst, const char *src, size_t size); + #endif --- kadu/kadu-core/misc.cpp 2007-03-26 00:51:05.000000000 +0200 +++ kadu/kadu-core/misc.cpp 2007-03-26 01:09:52.000000000 +0200 @@ -175,8 +175,7 @@ kdebugf2(); return NULL; } - strncat(path, argv0 + 1, len - 1); - path[len - 1] = 0; + strlcat(path, argv0 + 1, len); if (!delinkify(path, len)) { kdebugf2(); @@ -196,9 +195,8 @@ kdebugf2(); return NULL; } - strncat(path, "/", len - 1); - strncat(path, argv0, len - 1); - path[len - 1] = 0; + strncat(path, "/", len); + strncat(path, argv0, len); if (!delinkify(path, len)) { kdebugf2(); @@ -212,8 +210,7 @@ if (argv0[0] == '/') //ścieżka bezwzględna { - strncpy(path, argv0, len - 1); - path[len - 1] = 0; + strlcpy(path, argv0, len); if (!delinkify(path, len)) { kdebugf2(); @@ -239,8 +236,7 @@ memcpy(path, previous, l); path[l] = '/'; path[l + 1] = 0; - strncat(path, argv0, len); - path[len - 1] = 0; + strlcat(path, argv0, len); if (!delinkify(path, len)) { kdebugf2(); @@ -258,14 +254,12 @@ previous = current + 1; } //nie znaleźliśmy dotąd (bo szukaliśmy ':'), więc może w pozostałej części coś się znajdzie? - strncpy(path, previous, len - 2); - path[len - 2] = 0; + strlcpy(path, previous, len - 1); l = strlen(path); path[l] = '/'; path[l + 1] = 0; - strncat(path, argv0, len); - path[len - 1] = 0; + strlcat(path, argv0, len); if (!delinkify(path, len)) { kdebugf2(); @@ -1048,3 +1042,43 @@ kdebugf2(); return r; } + + +size_t strlcat(char *dst, const char *src, size_t size) +{ + register size_t i, j; + size_t left, dlen; + + for (i = 0; i < size && dst[i]; i++) + continue; + + dlen = i; + left = size - i; + + for (j = 0; left > j + 1 && src[j]; j++, i++) + dst[i] = src[j]; + + if (left) + dst[i] = 0; + + while (src[j]) + j++; + + return dlen + j; +} + +size_t strlcpy(char *dst, const char *src, size_t size) +{ + register size_t i, n = size; + + for (i = 0; n > 1 && src[i]; i++, n--) + dst[i] = src[i]; + + if (n) + dst[i] = 0; + + while (src[i]) + i++; + + return i; +}