From 271a1f830acb229c380ae95c6c2e4c4df367fa82 Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Mon, 2 Dec 2019 15:39:30 -0500 Subject: Beef up XDR unit tests. --- xdr.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'xdr.c') diff --git a/xdr.c b/xdr.c index 92c2b64..3c02635 100644 --- a/xdr.c +++ b/xdr.c @@ -94,6 +94,9 @@ hal_error_t hal_xdr_encode_fixed_opaque(uint8_t ** const outbuf, const uint8_t * /* arg checks */ hal_assert(outbuf != NULL && *outbuf != NULL && limit != NULL && limit >= *outbuf && (value != NULL || len == 0)); + if (len == 0) + return HAL_OK; + /* buffer overflow check */ /* We need to explicitly check (len > 0xfffffffc) because padding will * round it up to 0. @@ -135,11 +138,22 @@ hal_error_t hal_xdr_decode_fixed_opaque(const uint8_t ** const inbuf, const uint const uint8_t *p; hal_error_t err; + /* arg checks */ + hal_assert(value != NULL || len == 0); + + if (len == 0) + return HAL_OK; + /* get and advance the input data pointer */ - if ((err = hal_xdr_decode_fixed_opaque_ptr(inbuf, limit, &p, len)) == HAL_OK) + if ((err = hal_xdr_decode_fixed_opaque_ptr(inbuf, limit, &p, len)) == HAL_OK) { /* read the data */ memcpy(value, p, len); + /* pad if necessary */ + for (size_t i = len; (i & 3) != 0; ++i) + value[i] = 0; + } + return err; } @@ -204,8 +218,14 @@ hal_error_t hal_xdr_decode_variable_opaque(const uint8_t ** const inbuf, const u /* user buffer overflow check */ if (len_max < xdr_len) return HAL_ERROR_XDR_BUFFER_OVERFLOW; + /* read the data */ memcpy(value, p, xdr_len); + + /* pad if necessary */ + for (size_t i = xdr_len; (i & 3) != 0; ++i) + value[i] = 0; + *len = xdr_len; } -- cgit v1.2.3