Project

General

Profile

Review #751 » 0003-sanitize-bip_strcat-f-_fit-cleanup.patch

Better code for previous patch (strict cflags etc.) - Loïc Gomez, 2022-01-10 19:21

View differences:

src/util.c
return r;
}
char *bip_strcat_fit(size_t *remaining, char *str, char *str2)
char *bip_strcat_fit(size_t *remaining, char *str, const char *str2)
{
char *res;
......
return res;
}
char *bip_strcatf_fit(size_t *remaining, char *str, char *fmt, ...)
#define STRCATF_BUF_MAXLEN 1024
char *bip_strcatf_fit(size_t *remaining, char *str, const char *fmt, ...)
{
va_list ap;
char str2[*remaining];
size_t written;
char str2[STRCATF_BUF_MAXLEN + 1];
int written;
char *res = NULL;
if (!remaining || !str || !fmt) {
......
return NULL;
}
if (*remaining > STRCATF_BUF_MAXLEN) {
mylog(LOG_ERROR, "bip_strcatf_fit: remaining "
"is over STRCATF_BUF_MAXLEN");
}
va_start(ap, fmt);
str2[*remaining] = '\0';
written = vsnprintf(str2, *remaining, fmt, ap);
if (written >= *remaining) {
if (written < 0) {
mylog(LOG_ERROR, "bip_strcatf_fit: vsnprintf failed with: %s",
strerror(errno));
return NULL;
}
if ((unsigned)written >= *remaining) {
mylog(LOG_DEBUGVERB, "bip_strcatf_fit,vsnprintf: no space left");
goto end;
}
res = memccpy(str, str2, '\0', *remaining);
if (!res) {
mylog(LOG_DEBUGTOOMUCH, "bip_strcatf_fit: memccpy() failed, remaining %lu",
*remaining);
mylog(LOG_DEBUGTOOMUCH, "bip_strcatf_fit: memccpy() failed, "
"remaining %lu", *remaining);
goto end;
}
src/util.h
void *bip_calloc(size_t nmemb, size_t size);
void *bip_realloc(void *ptr, size_t size);
char *bip_strdup(const char *str);
char *bip_strcat_fit(size_t *remaining, char *str, char *str2);
char *bip_strcatf_fit(size_t *remaining, char *str, char *str2, ...);
char *bip_strcat_fit(size_t *remaining, char *str, const char *str2);
char *bip_strcatf_fit(size_t *remaining, char *str, const char *str2, ...);
#define array_each(a, idx, ptr) for ((idx) = 0; \
(idx) < (a)->elemc && (((ptr) = array_get((a), (idx))) || 1); \
(idx)++)
(4-4/4)