Commit:    e36adfe94a663bc1eeb5d9d378dc80883de179db
Author:    Boris Lytochkin <lytbo...@php.net>         Fri, 3 May 2013 15:35:09 
+0400
Parents:   a39282b64dad04a047f0ff8c230a5a64ad417a2f
Branches:  PHP-5.4 PHP-5.5 master

Link:       
http://git.php.net/?p=php-src.git;a=commitdiff;h=e36adfe94a663bc1eeb5d9d378dc80883de179db

Log:
Fixed bug #64159 (Truncated snmpget)

Bugs:
https://bugs.php.net/64159

Changed paths:
  M  NEWS
  M  ext/snmp/snmp.c


Diff:
diff --git a/NEWS b/NEWS
index b6dc09c..dba1508 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,7 @@ PHP                                                           
             NEWS
 - SNMP:
   . Fixed bug #64765 (Some IPv6 addresses get interpreted wrong).
        (Boris Lytochkin)
+  . Fixed bug #64159 (Truncated snmpget). (Boris Lytochkin)
 
 ?? ??? 2013, PHP 5.4.15
 - Core:
diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c
index fad5d05..9d854ec 100644
--- a/ext/snmp/snmp.c
+++ b/ext/snmp/snmp.c
@@ -561,25 +561,50 @@ static void php_snmp_getvalue(struct variable_list *vars, 
zval *snmpval TSRMLS_D
        int buflen = sizeof(sbuf) - 1;
        int val_len = vars->val_len;
        
-       if ((valueretrieval & SNMP_VALUE_PLAIN) == 0) {
-               val_len += 32; /* snprint_value will add type info into value, 
make some space for it */
+       /* use emalloc() for large values, use static array otherwize */
+
+       /* There is no way to know the size of buffer snprint_value() needs in 
order to print a value there.
+        * So we are forced to probe it
+        */
+       while ((valueretrieval & SNMP_VALUE_PLAIN) == 0) {
+               *buf = '\0';
+               if (snprint_value(buf, buflen, vars->name, vars->name_length, 
vars) == -1) {
+                        /* buffer is not long enough to hold full output, 
double it */
+                       val_len *= 2;
+               } else {
+                       break;
+               }
+
+               if (buf == dbuf) {
+                       dbuf = (char *)erealloc(dbuf, val_len + 1);
+               } else {
+                       dbuf = (char *)emalloc(val_len + 1);
+               }
+
+               if (!dbuf) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() 
failed: %s, fallback to static buffer", strerror(errno));
+                       buf = &(sbuf[0]);
+                       buflen = sizeof(sbuf) - 1;
+                       break;
+               }
+
+               buf = dbuf;
+               buflen = val_len;
        }
 
-       /* use emalloc() for large values, use static array otherwize */
-       if(val_len > buflen){
+       if((valueretrieval & SNMP_VALUE_PLAIN) && val_len > buflen){
                if ((dbuf = (char *)emalloc(val_len + 1))) {
                        buf = dbuf;
                        buflen = val_len;
                } else {
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() 
failed: %s, fallback to static array", strerror(errno));
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "emalloc() 
failed: %s, fallback to static buffer", strerror(errno));
                }
        }
 
-       *buf = 0;
-
        MAKE_STD_ZVAL(val);
 
        if (valueretrieval & SNMP_VALUE_PLAIN) {
+               *buf = 0;
                switch (vars->type) {
                case ASN_BIT_STR:               /* 0x03, asn1.h */
                        ZVAL_STRINGL(val, (char *)vars->val.bitstring, 
vars->val_len, 1);
@@ -652,7 +677,7 @@ static void php_snmp_getvalue(struct variable_list *vars, 
zval *snmpval TSRMLS_D
                        break;
                }
        } else /* use Net-SNMP value translation */ {
-               snprint_value(buf, buflen, vars->name, vars->name_length, vars);
+               /* we have desired string in buffer, just use it */
                ZVAL_STRING(val, buf, 1);
        }


--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to