OSDN Git Service

DO NOT MERGE Truncate new line characters when adding string to config
authorHansong Zhang <hsz@google.com>
Fri, 9 Feb 2018 22:16:59 +0000 (14:16 -0800)
committerMoritz Horstmann <dev@peterzweg.at>
Sun, 8 Apr 2018 22:32:15 +0000 (00:32 +0200)
Bug: 70808273
Test: test with a device with newline character in name
Change-Id: I8729e12ad5851ee1ffbcb7c08e9a659f768ffc21
(cherry picked from commit dd9bbfc2458569d9fecf35f7503d1b89b4c69aa0)
(cherry picked from commit 7f8bfcc35285ca6e93a4436699bc95c13b920caf)
mh0rst: Port to C

osi/src/config.c

index 8a3e230..59bc5e2 100644 (file)
@@ -34,6 +34,7 @@
 #include "osi/include/allocator.h"
 #include "osi/include/list.h"
 #include "osi/include/log.h"
+#include "log/log.h"
 
 typedef struct {
   char *key;
@@ -221,16 +222,36 @@ void config_set_string(config_t *config, const char *section, const char *key, c
   }
 
   if (sec) {
+    char *value_string = (char *)value;
+    char *value_no_newline;
+    char *newline = strstr(value_string, "\n");
+    if (newline) {
+      android_errorWriteLog(0x534e4554, "70808273");
+      size_t newline_pos = newline - value_string;
+      value_no_newline = osi_strndup(value_string, newline_pos);
+      if (!value_no_newline) {
+        LOG_ERROR(LOG_TAG,"%s: Unable to allocate memory for value_no_newline", __func__);
+        return;
+      }
+    } else {
+      value_no_newline = value_string;
+    }
     for (const list_node_t *node = list_begin(sec->entries); node != list_end(sec->entries); node = list_next(node)) {
       entry_t *entry = list_node(node);
       if (!strcmp(entry->key, key)) {
         osi_free(entry->value);
-        entry->value = osi_strdup(value);
+        entry->value = osi_strdup(value_no_newline);
+        if (newline) {
+          osi_free(value_no_newline);
+        }
         return;
       }
     }
 
-    entry_t *entry = entry_new(key, value);
+    entry_t *entry = entry_new(key, value_no_newline);
+    if (newline) {
+      osi_free(value_no_newline);
+    }
     list_append(sec->entries, entry);
   }
 }