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.
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;
+
+ p = ret = malloc(l);
+ if (!ret)
return NULL;
- for (unsigned i = 0; i < n; i++)
+ 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) {
})
#define strprepend(x, ...) strprepend_with_separator(x, NULL, __VA_ARGS__)
-char* strrep(const char *s, unsigned n);
+char* strrep(const char *s, size_t n);
#define strrepa(s, n) \
({ \
ASSERT_STREQ(onea, "waldo");
ASSERT_STREQ(threea, "waldowaldowaldo");
+
+ ASSERT_NULL(strrep("waldo", SIZE_MAX - 1));
}
TEST(string_has_cc) {