From: Arran Cudbard-Bell Date: Wed, 22 Jun 2022 20:15:08 +0000 (-0500) Subject: sbuff->shifted should be reinitialised to 0 with each new sbuff in the chain X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd8e20b25c3bcef5b451824d1e3cea06a3ebe7de;p=thirdparty%2Ffreeradius-server.git sbuff->shifted should be reinitialised to 0 with each new sbuff in the chain This breaks the file streaming sbuff, so record the shifted count separately in the fr_sbuff_uctx_file_t structure --- diff --git a/src/lib/util/sbuff.c b/src/lib/util/sbuff.c index 2c0cdbc7884..d8288c75566 100644 --- a/src/lib/util/sbuff.c +++ b/src/lib/util/sbuff.c @@ -266,7 +266,7 @@ size_t fr_sbuff_extend_file(fr_sbuff_t *sbuff, size_t extension) if (extension == SIZE_MAX) extension = 0; - total_read = sbuff->shifted + (sbuff->end - sbuff->buff); + total_read = fctx->shifted + (sbuff->end - sbuff->buff); if (total_read >= fctx->max) { fr_strerror_const("Can't satisfy extension request, max bytes read"); return 0; /* There's no way we could satisfy the extension request */ @@ -279,7 +279,7 @@ size_t fr_sbuff_extend_file(fr_sbuff_t *sbuff, size_t extension) * * Note: p and markers are constraints here. */ - fr_sbuff_shift(sbuff, fr_sbuff_used(sbuff)); + fctx->shifted += fr_sbuff_shift(sbuff, fr_sbuff_used(sbuff)); } available = fctx->buff_end - sbuff->end; diff --git a/src/lib/util/sbuff.h b/src/lib/util/sbuff.h index ef7c5d401f5..77c5bbc3fae 100644 --- a/src/lib/util/sbuff.h +++ b/src/lib/util/sbuff.h @@ -124,6 +124,7 @@ typedef struct { FILE *file; //!< FILE * we're reading from. char *buff_end; //!< The true end of the buffer. size_t max; //!< Maximum number of bytes to read. + size_t shifted; //!< How much we've read from this file. bool eof; //!< are we at EOF? } fr_sbuff_uctx_file_t; @@ -406,7 +407,7 @@ do { \ .p = (_current), \ .is_const = fr_sbuff_ptr(_sbuff_or_marker)->is_const, \ .adv_parent = (_adv_parent), \ - .shifted = fr_sbuff_ptr(_sbuff_or_marker)->shifted, \ + .shifted = 0, \ .extend = (_extend), \ .uctx = fr_sbuff_ptr(_sbuff_or_marker)->uctx, \ .parent = fr_sbuff_ptr(_sbuff_or_marker) \ diff --git a/src/lib/util/sbuff_tests.c b/src/lib/util/sbuff_tests.c index abc75d8c194..754bda62a1d 100644 --- a/src/lib/util/sbuff_tests.c +++ b/src/lib/util/sbuff_tests.c @@ -1042,8 +1042,8 @@ static void test_file_extend_max(void) TEST_CHECK(fr_sbuff_init_file(&sbuff, &fctx, buff, sizeof(buff), fp, sizeof(fbuff) - 8) == &sbuff); TEST_CASE("Confirm that max stops us from seeing xyzzy"); - TEST_CHECK_LEN(sizeof(fbuff) - 8, fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, NULL)); - (void) fr_sbuff_out_abstrncpy(NULL, &post_ws, &sbuff, 24); + TEST_CHECK_SLEN(fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, NULL), sizeof(fbuff) - 8); + TEST_CHECK_SLEN(fr_sbuff_out_abstrncpy(NULL, &post_ws, &sbuff, 24), 0); TEST_CHECK_STRCMP(post_ws, ""); talloc_free(post_ws); fclose(fp);