From d0787a2c86a80e31756965c436fac67b7d1c0f9b Mon Sep 17 00:00:00 2001
From: Bart Van Assche <bvanassche@acm.org>
Date: Fri, 11 Oct 2019 20:09:08 -0700
Subject: [PATCH] HOST-RESOURCES-MIB, UCD-SNMP-MIB: Report empty strings
 correctly

See also https://github.com/net-snmp/net-snmp/issues/26.

Fixes: 9b9c0e287b4d ("MIBs: Use asprintf() instead of snprintf() to prevent truncation")
---
 agent/mibgroup/host/hrh_filesys.c | 5 +++--
 agent/mibgroup/ucd-snmp/disk.c    | 3 ++-
 agent/mibgroup/ucd-snmp/disk_hw.c | 3 ++-
 agent/mibgroup/ucd-snmp/proc.c    | 4 ++--
 4 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/agent/mibgroup/host/hrh_filesys.c b/agent/mibgroup/host/hrh_filesys.c
index 354416157..073a37e98 100644
--- a/agent/mibgroup/host/hrh_filesys.c
+++ b/agent/mibgroup/host/hrh_filesys.c
@@ -219,6 +219,7 @@ var_hrhfilesys(struct variable *vp,
 {
     int             fsys_idx;
     static char    *string;
+    static char     empty_str[1];
 
     fsys_idx =
         header_hrhfilesys(vp, name, length, exact, var_len, write_method);
@@ -235,7 +236,7 @@ var_hrhfilesys(struct variable *vp,
         *var_len = 0;
         if (asprintf(&string, "%s", HRFS_entry->path) >= 0)
             *var_len = strlen(string);
-        return (u_char *) string;
+        return (u_char *)(string ? string : empty_str);
     case HRFSYS_RMOUNT:
         free(string);
         if (HRFS_entry->flags & NETSNMP_FS_FLAG_REMOTE) {
@@ -245,7 +246,7 @@ var_hrhfilesys(struct variable *vp,
             string = strdup("");
         }
         *var_len = string ? strlen(string) : 0;
-        return (u_char *) string;
+        return (u_char *)(string ? string : empty_str);
 
     case HRFSYS_TYPE:
         fsys_type_id[fsys_type_len - 1] = 
diff --git a/agent/mibgroup/ucd-snmp/disk.c b/agent/mibgroup/ucd-snmp/disk.c
index d827dcc18..52062352e 100644
--- a/agent/mibgroup/ucd-snmp/disk.c
+++ b/agent/mibgroup/ucd-snmp/disk.c
@@ -825,6 +825,7 @@ var_extensible_disk(struct variable *vp,
     struct dsk_entry entry;
     static long     long_ret;
     static char    *errmsg;
+    static char     empty_str[1];
 
 tryAgain:
     if (header_simple_table
@@ -926,7 +927,7 @@ var_extensible_disk(struct variable *vp,
                 *var_len = strlen(errmsg);
             }
         }
-        return (u_char *) (errmsg);
+        return (u_char *)(errmsg ? errmsg : empty_str);
     }
     return NULL;
 }
diff --git a/agent/mibgroup/ucd-snmp/disk_hw.c b/agent/mibgroup/ucd-snmp/disk_hw.c
index cc5da14de..e8b09a238 100644
--- a/agent/mibgroup/ucd-snmp/disk_hw.c
+++ b/agent/mibgroup/ucd-snmp/disk_hw.c
@@ -314,6 +314,7 @@ var_extensible_disk(struct variable *vp,
     unsigned long long val;
     static long     long_ret;
     static char    *errmsg;
+    static char     empty_str[1];
     netsnmp_cache  *cache;
 
     /* Update the fsys H/W module */
@@ -432,7 +433,7 @@ var_extensible_disk(struct variable *vp,
              >= 0)) {
             *var_len = strlen(errmsg);
         }
-        return (u_char *) errmsg;
+        return (u_char *)(errmsg ? errmsg : empty_str);
     }
     return NULL;
 }
diff --git a/agent/mibgroup/ucd-snmp/proc.c b/agent/mibgroup/ucd-snmp/proc.c
index 57aa2d58a..8eb5fa7ac 100644
--- a/agent/mibgroup/ucd-snmp/proc.c
+++ b/agent/mibgroup/ucd-snmp/proc.c
@@ -265,7 +265,7 @@ var_extensible_proc(struct variable *vp,
     struct myproc  *proc;
     static long     long_ret;
     static char    *errmsg;
-
+    static char     empty_str[1];
 
     if (header_simple_table
         (vp, name, length, exact, var_len, write_method, numprocs))
@@ -328,7 +328,7 @@ var_extensible_proc(struct variable *vp,
                 }
             }
             *var_len = errmsg ? strlen(errmsg) : 0;
-            return ((u_char *) errmsg);
+            return (u_char *)(errmsg ? errmsg : empty_str);
         case ERRORFIX:
             *write_method = fixProcError;
             long_return = fixproc.result;
