sanitize: a bit evolved size_t cast for get_str_elem

This commit is contained in:
Loïc Gomez 2022-01-09 21:24:25 +01:00 committed by Pierre-Louis Bonicoli
parent 79d9be4e71
commit efb79b1e80
Signed by: pilou
GPG Key ID: 06914C4A5EDAA6DD
1 changed files with 12 additions and 8 deletions

View File

@ -721,25 +721,29 @@ static char *get_str_elem(char *str, int num)
int index = 0; int index = 0;
while ((c = strchr(cur, PASS_SEP))) { while ((c = strchr(cur, PASS_SEP))) {
long len = c - cur;
if (index < num) { if (index < num) {
index++; index++;
cur = c + 1; cur = c + 1;
continue; continue;
} }
if (c - cur < 1) if (len < 1)
return NULL; return NULL;
ret = bip_malloc(c - cur + 1); // len always > 0
memcpy(ret, cur, c - cur); ret = bip_malloc((size_t)len + 1);
ret[c - cur] = 0; memcpy(ret, cur, (size_t)len);
ret[len] = 0;
return ret; return ret;
} }
if (index == num) { if (index == num) {
long len;
c = str + strlen(str); c = str + strlen(str);
if (c - cur < 1) len = c - cur;
if (len < 1)
return NULL; return NULL;
ret = bip_malloc(c - cur + 1); ret = bip_malloc((size_t)len + 1);
memcpy(ret, cur, c - cur); memcpy(ret, cur, (size_t)len);
ret[c - cur] = 0; ret[len] = 0;
return ret; return ret;
} }
return NULL; return NULL;