From: Jan Ekström Date: Thu, 8 Jan 2015 15:29:04 +0000 (+0200) Subject: {common,resource}: make sure buffers are null-terminated. X-Git-Tag: v0.0.74^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=188dba83d7256cf37c0094231c685ae3396532ee;p=profile%2Fivi%2Fmurphy.git {common,resource}: make sure buffers are null-terminated. These were found by Coverity, and technically they are correct. Thus explicit null termination is added or strncpy is used. --- diff --git a/src/common/process.c b/src/common/process.c index e9728cf..71a10e1 100644 --- a/src/common/process.c +++ b/src/common/process.c @@ -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; diff --git a/src/common/tests/mainloop-test.c b/src/common/tests/mainloop-test.c index 658cf47..77dea36 100644 --- a/src/common/tests/mainloop-test.c +++ b/src/common/tests/mainloop-test.c @@ -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); } diff --git a/src/resource/lua-resource.c b/src/resource/lua-resource.c index 736640d..928c362 100644 --- a/src/resource/lua-resource.c +++ b/src/resource/lua-resource.c @@ -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: