From 678fa2e92311ff80c64d2a8e536f6c4126614660 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 5 Sep 2014 15:54:48 +0200 Subject: [PATCH] network plugin: Improve "Buffer too small" error message. References: #724 --- src/network.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/network.c b/src/network.c index b1919833..979d57d0 100644 --- a/src/network.c +++ b/src/network.c @@ -917,15 +917,19 @@ static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len, } /* int parse_part_number */ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len, - char *output, int output_len) + char *output, size_t const output_len) { char *buffer = *ret_buffer; size_t buffer_len = *ret_buffer_len; uint16_t tmp16; - size_t header_size = 2 * sizeof (uint16_t); + size_t const header_size = 2 * sizeof (uint16_t); uint16_t pkg_length; + size_t payload_size; + + if (output_len <= 0) + return (EINVAL); if (buffer_len < header_size) { @@ -944,6 +948,7 @@ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len, memcpy ((void *) &tmp16, buffer, sizeof (tmp16)); buffer += sizeof (tmp16); pkg_length = ntohs (tmp16); + payload_size = ((size_t) pkg_length) - header_size; /* Check that packet fits in the input buffer */ if (pkg_length > buffer_len) @@ -969,22 +974,24 @@ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len, /* Check that the package data fits into the output buffer. * The previous if-statement ensures that: * `pkg_length > header_size' */ - if ((output_len < 0) - || ((size_t) output_len < ((size_t) pkg_length - header_size))) + if (output_len < payload_size) { WARNING ("network plugin: parse_part_string: " - "Output buffer too small."); + "Buffer too small: " + "Output buffer holds %zu bytes, " + "which is too small to hold the received " + "%zu byte string.", + output_len, payload_size); return (-1); } /* All sanity checks successfull, let's copy the data over */ - output_len = pkg_length - header_size; - memcpy ((void *) output, (void *) buffer, output_len); - buffer += output_len; + memcpy ((void *) output, (void *) buffer, payload_size); + buffer += payload_size; /* For some very weird reason '\0' doesn't do the trick on SPARC in * this statement. */ - if (output[output_len - 1] != 0) + if (output[payload_size - 1] != 0) { WARNING ("network plugin: parse_part_string: " "Received string does not end " -- 2.11.0