*/
size_t fr_sbuff_extend_file(fr_sbuff_t *sbuff, size_t extension)
{
- size_t read, available;
+ size_t read, available, total_read;
fr_sbuff_uctx_file_t *fctx;
CHECK_SBUFF_INIT(sbuff);
if (extension == SIZE_MAX) extension = 0;
+ total_read = sbuff->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 */
+ }
+
if (fr_sbuff_used(sbuff)) {
/*
* Try and shift as much as we can out
}
available = fctx->buff_end - sbuff->end;
+ if (available > (fctx->max - total_read)) available = fctx->max - total_read;
if (available < extension) {
fr_strerror_printf("Can't satisfy extension request for %zu bytes", extension);
return 0; /* There's no way we could satisfy the extension request */
(status & FR_SBUFF_EXTEND_ERROR) == 0) {
return true;
}
-
+
return false;
}
.p = (_sbuff)->p, \
.is_const = (_sbuff)->is_const, \
.extend = (_sbuff)->extend, \
+ .shifted = (_sbuff)->shifted, \
.uctx = (_sbuff)->uctx, \
.parent = (_sbuff) \
})
.is_const = (_sbuff)->is_const, \
.adv_parent = 1, \
.extend = (_sbuff)->extend, \
+ .shifted = (_sbuff)->shifted, \
.uctx = (_sbuff)->uctx, \
.parent = (_sbuff) \
})
fclose(fp);
}
+static void test_file_extend_max(void)
+{
+ fr_sbuff_t sbuff;
+ fr_sbuff_uctx_file_t fctx;
+ FILE *fp;
+ char buff[16];
+ char fbuff[] = " xyzzy";
+ char *post_ws;
+
+ TEST_CASE("Initialization");
+ fp = fmemopen(fbuff, sizeof(fbuff) - 1, "r");
+ TEST_CHECK(fp != NULL);
+ 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_STRCMP(post_ws, "");
+ talloc_free(post_ws);
+ fclose(fp);
+}
+
static void test_adv_past_str(void)
{
fr_sbuff_t sbuff;
{ "fr_sbuff_talloc_extend_multi_level", test_talloc_extend_multi_level },
{ "fr_sbuff_talloc_extend_with_marker", test_talloc_extend_with_marker },
{ "fr_sbuff_file_extend", test_file_extend },
+ { "fr_sbuff_file_extend_max", test_file_extend_max },
{ "fr_sbuff_no_advance", test_no_advance },