Ensure nbase's hexdump doesn't print too much

This commit is contained in:
dmiller 2026-05-06 21:38:09 +00:00
parent c94d96b65c
commit 323d875df8
3 changed files with 17 additions and 5 deletions

View file

@ -626,6 +626,10 @@ char *hexdump(const u8 *cp, u32 length){
}
}
/* Allocate enough space to print the hex dump */
if (length > 16 * ((INT_MAX - 1) / LINE_LEN - 1)) {
/* Too big; would overflow INT_MAX bytes! */
return NULL;
}
bytes2alloc=(length%16==0)? (1 + LINE_LEN * (length/16)) : (1 + LINE_LEN * (1+(length/16))) ;
buffer=(char *)safe_zalloc(bytes2alloc);
current_line=buffer;

View file

@ -1682,8 +1682,10 @@ void ProbeMode::probe_nping_event_handler(nsock_pool nsp, nsock_event nse, void
snprintf(final_output, sizeof(final_output), "RCVD (%.4fs) %s\n", o.stats.elapsedRuntime(t), buffer);
if( o.getVerbosity() >= VB_3 ){
hex=hexdump(packet, packetlen);
strncat(final_output, hex, sizeof(final_output)-1);
free(hex);
if (hex) {
strncat(final_output, hex, sizeof(final_output)-1);
free(hex);
}
}
prevtime=pcaptime;
@ -1710,8 +1712,10 @@ void ProbeMode::probe_nping_event_handler(nsock_pool nsp, nsock_event nse, void
snprintf(final_output, sizeof(final_output), "RCVD (%.4fs) %s\n", o.stats.elapsedRuntime(t), buffer);
if( o.getVerbosity() >= VB_3 ){
hex=hexdump(packet, packetlen);
strncat(final_output, hex, sizeof(final_output)-1);
free(hex);
if (hex) {
strncat(final_output, hex, sizeof(final_output)-1);
free(hex);
}
}
prevtime=pcaptime;
o.stats.addRecvPacket(packetlen);

View file

@ -546,7 +546,11 @@ const struct MatchDetails *ServiceProbeMatch::testMatch(const u8 *buf, int bufle
pcre2_get_error_message(rc, (unsigned char *)info, SERVICE_EXTRA_LEN);
error("PCRE2 error message: %s", info);
if (o.debugging > 1) {
error("Service data: \n%s", hexdump(buf, buflen));
char *hex = hexdump(buf, buflen);
if (hex) {
error("Service data: \n%s", hex);
free(hex);
}
}
}
}