diff options
author | Boris Lytochkin <lytboris@php.net> | 2013-05-03 15:39:57 +0400 |
---|---|---|
committer | Boris Lytochkin <lytboris@php.net> | 2013-05-03 15:39:57 +0400 |
commit | c68f9d5503dda84326cfdde4c5e9c51d077aabd0 (patch) | |
tree | cdc289699e713794d536e815fd2ee6b49ec6da27 /ext | |
parent | cbe2870b72c4cfdb5c295e83c88d7cff5c39e396 (diff) | |
parent | e36adfe94a663bc1eeb5d9d378dc80883de179db (diff) | |
download | php-git-c68f9d5503dda84326cfdde4c5e9c51d077aabd0.tar.gz |
Merge branch 'PHP-5.4' into PHP-5.5
* PHP-5.4:
Fixed bug #64159 (Truncated snmpget)
Diffstat (limited to 'ext')
-rw-r--r-- | ext/snmp/snmp.c | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index cd96be0d5d..756e5310eb 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -576,25 +576,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); @@ -667,7 +692,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); } |