basic/string-util: simplify how str_realloc() is used

All callers ignore failure anyway, so let's do that internally.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-05-05 12:12:42 +02:00
parent a01080ceb3
commit 523e1b14a1
3 changed files with 7 additions and 13 deletions
+1 -1
View File
@@ -179,7 +179,7 @@ int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags
if (!ans)
return -ENOMEM;
(void) str_realloc(&ans);
ans = str_realloc(ans);
*line = TAKE_PTR(ans);
return 0;
}
+4 -8
View File
@@ -217,17 +217,13 @@ static inline void *memory_startswith_no_case(const void *p, size_t sz, const ch
return (uint8_t*) p + n;
}
static inline char* str_realloc(char **p) {
/* Reallocate *p to actual size */
static inline char* str_realloc(char *p) {
/* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
if (!*p)
if (!p)
return NULL;
char *t = realloc(*p, strlen(*p) + 1);
if (!t)
return NULL;
return (*p = t);
return realloc(p, strlen(p) + 1) ?: p;
}
char* string_erase(char *x);
+2 -4
View File
@@ -196,8 +196,7 @@ char *utf8_escape_invalid(const char *str) {
}
*s = '\0';
(void) str_realloc(&p);
return p;
return str_realloc(p);
}
static int utf8_char_console_width(const char *str) {
@@ -282,8 +281,7 @@ char *utf8_escape_non_printable_full(const char *str, size_t console_width) {
finish:
*s = '\0';
(void) str_realloc(&p);
return p;
return str_realloc(p);
}
char *ascii_is_valid(const char *str) {