]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/string-util: simplify how str_realloc() is used
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 11 Mar 2021 10:40:57 +0000 (11:40 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 5 May 2021 10:12:42 +0000 (12:12 +0200)
All callers ignore failure anyway, so let's do that internally.

src/basic/process-util.c
src/basic/string-util.h
src/basic/utf8.c

index 7d4301eadb8a3388689829a22fb23ce0660f6bd5..902265ac21e4518b389eb64702957eaed4ed510c 100644 (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;
 }
index 0dafe049ad131b862d063d63e0c9a56cb2372789..1bc131f15b0a735d8fc58a37b19216020d9aadd1 100644 (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);
index 46c3a463b95ccd48fcd03a2a3e72e1c6f41130f9..244b8ade932de0b8e65b79c39ffc885256810d48 100644 (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) {