Skip to content

Commit 7ea2f28

Browse files
committed
readfile and textxform functions do not rely on 4k limit anymore
1 parent 5153e7c commit 7ea2f28

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

libpromises/evalfunction.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,13 +1415,21 @@ static FnCallResult FnCallCanonify(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const
14151415

14161416
static FnCallResult FnCallTextXform(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const Policy *policy, const FnCall *fp, const Rlist *finalargs)
14171417
{
1418-
char buf[CF_BUFSIZE];
1418+
char *buf = NULL;
14191419
char *string = RlistScalarValue(finalargs);
1420-
int len = 0;
1420+
FnCallResult ret;
1421+
size_t len = strlen(string);
1422+
size_t bufsiz = len + 1 < 2 ? 2 : len + 1;
1423+
1424+
if (len > CF_INFINITY)
1425+
{
1426+
free(buf);
1427+
Log(LOG_LEVEL_ERR, "%s: unable to parse without truncating", fp->name);
1428+
return FnFailure();
1429+
}
14211430

1422-
memset(buf, 0, sizeof(buf));
1423-
strlcpy(buf, string, sizeof(buf));
1424-
len = strlen(buf);
1431+
buf = xcalloc(bufsiz, sizeof(char));
1432+
(void)strlcpy(buf, string, bufsiz);
14251433

14261434
if (!strcmp(fp->name, "string_downcase"))
14271435
{
@@ -1451,12 +1459,12 @@ static FnCallResult FnCallTextXform(ARG_UNUSED EvalContext *ctx, ARG_UNUSED cons
14511459
}
14521460
else if (!strcmp(fp->name, "string_length"))
14531461
{
1454-
xsnprintf(buf, sizeof(buf), "%d", len);
1462+
xsnprintf(buf, bufsiz, "%d", len);
14551463
}
14561464
else if (!strcmp(fp->name, "string_head"))
14571465
{
14581466
const long max = IntFromString(RlistScalarValue(finalargs->next));
1459-
if (max < sizeof(buf))
1467+
if (max < bufsiz)
14601468
{
14611469
buf[max] = '\0';
14621470
}
@@ -1466,16 +1474,19 @@ static FnCallResult FnCallTextXform(ARG_UNUSED EvalContext *ctx, ARG_UNUSED cons
14661474
const long max = IntFromString(RlistScalarValue(finalargs->next));
14671475
if (max < len)
14681476
{
1469-
strncpy(buf, string + len - max, sizeof(buf) - 1);
1477+
strncpy(buf, string + len - max, len);
14701478
}
14711479
}
14721480
else
14731481
{
14741482
Log(LOG_LEVEL_ERR, "text xform with unknown call function %s, aborting", fp->name);
1483+
free(buf);
14751484
return FnFailure();
14761485
}
14771486

1478-
return FnReturn(buf);
1487+
ret = FnReturn(buf);
1488+
free(buf);
1489+
return ret;
14791490
}
14801491

14811492
/*********************************************************************/
@@ -5239,15 +5250,15 @@ static FnCallResult FnCallReadFile(ARG_UNUSED EvalContext *ctx, ARG_UNUSED const
52395250
char *requested_max = RlistScalarValue(finalargs->next);
52405251
int maxsize = IntFromString(requested_max);
52415252

5242-
if (maxsize > CF_BUFSIZE)
5253+
if (maxsize > CF_INFINITY)
52435254
{
5244-
Log(LOG_LEVEL_INFO, "%s: requested max size %s is more than the internal limit " TOSTRING(CF_BUFSIZE), fp->name, requested_max);
5245-
maxsize = CF_BUFSIZE;
5255+
Log(LOG_LEVEL_INFO, "%s: requested max size %s is more than the internal limit " TOSTRING(CF_INFINITY), fp->name, requested_max);
5256+
maxsize = CF_INFINITY;
52465257
}
52475258

52485259
if (maxsize == 0)
52495260
{
5250-
maxsize = CF_BUFSIZE;
5261+
maxsize = CF_INFINITY;
52515262
}
52525263

52535264
// Read once to validate structure of file in itemlist

tests/acceptance/01_vars/02_functions/readfile.cf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ bundle agent check
2424
{
2525
vars:
2626
"sizes" ilist => { @(test.sizes) };
27-
"expected[0]" int => "4095";
27+
"expected[0]" int => "10240";
2828
"expected[1]" int => "1";
2929
"expected[4094]" int => "4094";
3030
"expected[4095]" int => "4095";
31-
"expected[4096]" int => "4095";
32-
"expected[4097]" int => "4095";
33-
"expected[99999999]" int => "4095";
31+
"expected[4096]" int => "4096";
32+
"expected[4097]" int => "4097";
33+
"expected[99999999]" int => "10240";
3434

3535
classes:
3636
"ok_$(sizes)" expression => strcmp("$(test.length_$(sizes))",

0 commit comments

Comments
 (0)