librz/util: guard allocation failure paths in string and vector helpers (#6217)

This commit is contained in:
Alok Kumar Mishra 2026-04-12 14:46:24 +05:30 committed by GitHub
parent a6a1f83819
commit b46029ddec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 2 deletions

View file

@ -2627,7 +2627,9 @@ RZ_API char *rz_str_arg_escape(const char *arg) {
}
}
str[dest_i] = '\0';
return realloc(str, (strlen(str) + 1) * sizeof(char));
char *trimmed = realloc(str, (strlen(str) + 1) * sizeof(char));
// keep the valid oversized buffer if realloc fails.
return trimmed ? trimmed : str;
}
// Unescape the string arg to its original format
@ -2678,7 +2680,9 @@ RZ_API char *rz_str_path_escape(const char *path) {
}
str[dest_i] = '\0';
return realloc(str, (strlen(str) + 1) * sizeof(char));
char *trimmed = realloc(str, (strlen(str) + 1) * sizeof(char));
// same as above, logic is similar to rz_str_uri_encode.
return trimmed ? trimmed : str;
}
RZ_API int rz_str_path_unescape(char *path) {

View file

@ -239,6 +239,10 @@ RZ_API bool rz_strbuf_append_n(RzStrBuf *sb, const char *s, size_t l) {
}
newlen *= 2;
p = realloc(sb->ptr, newlen);
// preserve the existing buffer on OOM
if (!p) {
return false;
}
memset((char *)p + sb->ptrlen, 0, newlen - sb->ptrlen);
} else {
allocated = false;

View file

@ -167,6 +167,9 @@ RZ_API bool rz_vector_clone_into(
RZ_API RZ_OWN RzVector *rz_vector_clone(
RZ_NONNULL RZ_BORROW RZ_IN const RzVector *vec) {
RzVector *dst = rz_vector_clonef(vec, NULL);
if (!dst) {
return NULL;
}
dst->free = NULL;
dst->free_user = NULL;
return dst;