mirror of https://github.com/xzeldon/htop.git
XUtils: add safe strncpy implementation
The standard strncpy fails to null-terminate the destination in case the source is longer than the passed size.
This commit is contained in:
parent
3715301fe3
commit
a118928dee
12
XUtils.c
12
XUtils.c
|
@ -193,6 +193,18 @@ char* String_readLine(FILE* fd) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t String_safeStrncpy(char *restrict dest, const char *restrict src, size_t size) {
|
||||||
|
assert(size > 0);
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
for (; i < size - 1 && src[i]; i++)
|
||||||
|
dest[i] = src[i];
|
||||||
|
|
||||||
|
dest[i] = '\0';
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
int xAsprintf(char** strp, const char* fmt, ...) {
|
int xAsprintf(char** strp, const char* fmt, ...) {
|
||||||
va_list vl;
|
va_list vl;
|
||||||
va_start(vl, fmt);
|
va_start(vl, fmt);
|
||||||
|
|
3
XUtils.h
3
XUtils.h
|
@ -59,6 +59,9 @@ char* String_getToken(const char* line, unsigned short int numMatch);
|
||||||
|
|
||||||
char* String_readLine(FILE* fd);
|
char* String_readLine(FILE* fd);
|
||||||
|
|
||||||
|
/* Always null-terminates dest. Caller must pass a strictly positive size. */
|
||||||
|
size_t String_safeStrncpy(char *restrict dest, const char *restrict src, size_t size);
|
||||||
|
|
||||||
ATTR_FORMAT(printf, 2, 3)
|
ATTR_FORMAT(printf, 2, 3)
|
||||||
int xAsprintf(char** strp, const char* fmt, ...);
|
int xAsprintf(char** strp, const char* fmt, ...);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue