Before commit 7f05daa8e0e0 sprint_realloc_octet_string() used memcpy()
for ASCII strings. That caused the output to be truncated if a '\0' was
embedded in an octet string. Commit 7f05daa8e0e0 fixed that issue but
broke UTF-8 support. Restore UTF-8 support by only using
sprint_realloc_asciistring() if the octet string contains a '\0'.

Fixes: 7f05daa8e0e0 ("CHANGES: BUG: 3444939: BUG: 1796886: snmplib: Avoid that 
sprint_realloc_octet_string() embeds unprintable control characters or binary 
zeroes in its output. This behavior could cause truncated output in snmptrapd.")
---
 snmplib/mib.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/snmplib/mib.c b/snmplib/mib.c
index 6c72acf9326c..a16144065029 100644
--- a/snmplib/mib.c
+++ b/snmplib/mib.c
@@ -578,10 +578,20 @@ sprint_realloc_octet_string(u_char ** buf, size_t * 
buf_len,
                     break;
                 case 't': /* new in rfc 3411 */
                 case 'a':
+                    /* A string hint gives the max size - we may not need this 
much */
                     cnt = SNMP_MIN(width, ecp - cp);
-                    if (!sprint_realloc_asciistring(buf, buf_len, out_len,
-                                                    allow_realloc, cp, cnt))
+                    while ((*out_len + cnt + 1) > *buf_len) {
+                        if (!allow_realloc || !snmp_realloc(buf, buf_len))
+                            return 0;
+                    }
+                    if (memchr(cp, '\0', cnt) == NULL) {
+                        /* No embedded '\0' - use strlcpy() to preserve UTF-8 
*/
+                        strlcpy((char *)(*buf + *out_len), (char *)cp, cnt + 
1);
+                        *out_len += cnt;
+                    } else if (!sprint_realloc_asciistring(buf, buf_len,
+                                     out_len, allow_realloc, cp, cnt)) {
                         return 0;
+                    }
                     cp += cnt;
                     break;
                 default:
-- 
2.16.3


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to