{common,resource}: make sure buffers are null-terminated. upstream v0.0.74
authorJan Ekström <jan.ekstrom@intel.com>
Thu, 8 Jan 2015 15:29:04 +0000 (17:29 +0200)
committerKrisztian Litkey <krisztian.litkey@intel.com>
Thu, 8 Jan 2015 16:34:59 +0000 (18:34 +0200)
These were found by Coverity, and technically they are correct.
Thus explicit null termination is added or strncpy is used.

src/common/process.c
src/common/tests/mainloop-test.c
src/resource/lua-resource.c

index e9728cf..71a10e1 100644 (file)
@@ -177,7 +177,7 @@ static void process_change(mrp_io_watch_t *wd, int fd, mrp_io_event_t events,
                            void *user_data)
 {
     struct inotify_event *is;
-    int bufsize = sizeof(struct inotify_event) + PATH_MAX;
+    int bufsize = sizeof(struct inotify_event) + PATH_MAX + 1;
     char buf[bufsize];
     i_watch_t *w;
     FILE *f;
@@ -192,7 +192,7 @@ static void process_change(mrp_io_watch_t *wd, int fd, mrp_io_event_t events,
         int read_bytes;
         int processed_bytes = 0;
 
-        read_bytes = read(fd, buf, bufsize);
+        read_bytes = read(fd, buf, bufsize - 1);
 
         if (read_bytes < 0) {
             mrp_log_error("Failed to read event from inotify: %s",
@@ -200,6 +200,8 @@ static void process_change(mrp_io_watch_t *wd, int fd, mrp_io_event_t events,
             return;
         }
 
+        buf[read_bytes] = '\0';
+
         while (processed_bytes < read_bytes) {
             char *filename = NULL;
 
index 658cf47..77dea36 100644 (file)
@@ -1125,6 +1125,7 @@ static void setup_dbus_client(mrp_mainloop_t *ml)
     DBusConnection *conn;
     int             i, nmethod, nsignal;
     size_t          size;
+    ssize_t         amount_read;
 
     nmethod = cfg.ndbus_method;
     nsignal = cfg.ndbus_signal;
@@ -1142,8 +1143,10 @@ static void setup_dbus_client(mrp_mainloop_t *ml)
         if (i != dbus_test.pipe[0])
             close(i);
 
-    size = sizeof(dbus_test.address);
-    if (read(dbus_test.pipe[0], dbus_test.address, size) > 0) {
+    size = sizeof(dbus_test.address) - 1;
+    amount_read = read(dbus_test.pipe[0], dbus_test.address, size);
+    if (amount_read > 0) {
+        dbus_test.address[amount_read] = '\0';
         info("DBUS test: got address '%s'", dbus_test.address);
     }
 
index 736640d..928c362 100644 (file)
@@ -973,7 +973,8 @@ static int attribute_lua_stringify(lua_State *L)
 
         int keylen = strlen(attrs->name);
 
-        available -= keylen + 2;
+        /* we need space for 2 + null */
+        available -= keylen + 3;
 
         if (available < 0)
             goto outofspace;
@@ -981,8 +982,15 @@ static int attribute_lua_stringify(lua_State *L)
         strncpy(p, attrs->name, keylen);
         p += keylen;
 
-        strncpy(p, ": ", 2);
+        /*
+         * we copy ": \0" and then proceed to only
+         * move the pointer by two, thus we can
+         * add one to the amount of available
+         * space.
+         */
+        strncpy(p, ": ", 3);
         p += 2;
+        available += 1;
 
         switch (attrs->type) {
             case mqi_string: