netlink plugin: fix truncation warnings
authorRuben Kerkhof <ruben@rubenkerkhof.com>
Tue, 15 May 2018 19:05:27 +0000 (21:05 +0200)
committerRuben Kerkhof <ruben@rubenkerkhof.com>
Tue, 15 May 2018 19:54:35 +0000 (21:54 +0200)
  CC       src/netlink_la-netlink.lo
src/netlink.c: In function ‘qos_filter_cb’:
src/netlink.c:544:58: warning: ‘%s’ directive output may be truncated writing up to 127 bytes into a region of size between 121 and 122 [-Wformat-truncation=]
       snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
                                                          ^~            ~~~~~~~
src/netlink.c:544:7: note: ‘snprintf’ output between 7 and 135 bytes into a destination of size 128
       snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/netlink.c:577:58: warning: ‘%s’ directive output may be truncated writing up to 127 bytes into a region of size between 121 and 122 [-Wformat-truncation=]
       snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
                                                          ^~            ~~~~~~~
src/netlink.c:577:7: note: ‘snprintf’ output between 7 and 135 bytes into a destination of size 128
       snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  CCLD     netlink.la

src/netlink.c

index 0458547..0bd598c 100644 (file)
@@ -541,7 +541,13 @@ static int qos_filter_cb(const struct nlmsghdr *nlh, void *args) {
 
       stats_submitted = true;
 
 
       stats_submitted = true;
 
-      snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
+      int r = snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
+                       tc_inst);
+      if (r >= sizeof(type_instance)) {
+        ERROR("netlink plugin: type_instance truncated to %zu bytes, need %d",
+              sizeof(type_instance), r);
+        return MNL_CB_ERROR;
+      }
 
       if (q_stats.bs != NULL) {
         submit_one(dev, "ipt_bytes", type_instance, q_stats.bs->bytes);
 
       if (q_stats.bs != NULL) {
         submit_one(dev, "ipt_bytes", type_instance, q_stats.bs->bytes);
@@ -574,7 +580,13 @@ static int qos_filter_cb(const struct nlmsghdr *nlh, void *args) {
     if (!stats_submitted && ts != NULL) {
       char type_instance[DATA_MAX_NAME_LEN];
 
     if (!stats_submitted && ts != NULL) {
       char type_instance[DATA_MAX_NAME_LEN];
 
-      snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type, tc_inst);
+      int r = snprintf(type_instance, sizeof(type_instance), "%s-%s", tc_type,
+                       tc_inst);
+      if (r >= sizeof(type_instance)) {
+        ERROR("netlink plugin: type_instance truncated to %zu bytes, need %d",
+              sizeof(type_instance), r);
+        return MNL_CB_ERROR;
+      }
 
       submit_one(dev, "ipt_bytes", type_instance, ts->bytes);
       submit_one(dev, "ipt_packets", type_instance, ts->packets);
 
       submit_one(dev, "ipt_bytes", type_instance, ts->bytes);
       submit_one(dev, "ipt_packets", type_instance, ts->packets);