string-util: check for overflow in strrep()
This simply mirrors the same overflow check we already have in strrepa(), in case someone passed us a sufficiently long string. strrep() is currently used only in tests, so this is just hardening.
This commit is contained in:
+11
-6
@@ -977,22 +977,27 @@ oom:
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
char* strrep(const char *s, unsigned n) {
|
||||
char *r, *p;
|
||||
char* strrep(const char *s, size_t n) {
|
||||
char *ret, *p;
|
||||
size_t l;
|
||||
|
||||
assert(s);
|
||||
|
||||
l = strlen(s);
|
||||
p = r = malloc(l * n + 1);
|
||||
if (!r)
|
||||
if (!MUL_ASSIGN_SAFE(&l, n))
|
||||
return NULL;
|
||||
if (!INC_SAFE(&l, 1))
|
||||
return NULL;
|
||||
|
||||
for (unsigned i = 0; i < n; i++)
|
||||
p = ret = malloc(l);
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
for (size_t i = 0; i < n; i++)
|
||||
p = stpcpy(p, s);
|
||||
|
||||
*p = 0;
|
||||
return r;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second) {
|
||||
|
||||
Reference in New Issue
Block a user