tests: prefer dynamically allocated buffers

Help detecting Valgrind OOB reads and other issues.

 Conditional jump or move depends on uninitialised value(s)
   at 0x5452886: blobmsg_parse (blobmsg.c:203)
   by 0x400A8E: test_blobmsg (tests/test-blobmsg-parse.c:66)
   by 0x400A8E: main (tests/test-blobmsg-parse.c:82)

 Conditional jump or move depends on uninitialised value(s)
   at 0x545247F: blobmsg_check_name (blobmsg.c:39)
   by 0x545247F: blobmsg_check_attr_len (blobmsg.c:79)
   by 0x5452710: blobmsg_parse_array (blobmsg.c:159)
   by 0x400AB8: test_blobmsg (tests/test-blobmsg-parse.c:69)
   by 0x400AB8: main (tests/test-blobmsg-parse.c:82)

 Conditional jump or move depends on uninitialised value(s)
   at 0x54524A0: blobmsg_check_name (blobmsg.c:42)
   by 0x54524A0: blobmsg_check_attr_len (blobmsg.c:79)
   by 0x5452710: blobmsg_parse_array (blobmsg.c:159)
   by 0x400AB8: test_blobmsg (tests/test-blobmsg-parse.c:69)
   by 0x400AB8: main (tests/test-blobmsg-parse.c:82)

Ref: http://lists.infradead.org/pipermail/openwrt-devel/2020-January/021204.html
Signed-off-by: Petr Štetiar <ynezz@true.cz>
This commit is contained in:
Petr Štetiar 2020-01-18 18:32:55 +01:00
parent 1ffa415353
commit 5c0faaf4f5
5 changed files with 55 additions and 21 deletions

View file

@ -63,9 +63,9 @@ static void test_blobmsg_procd_instance(const char *filename)
{
#define BUF_LEN 2048
int r = 0;
FILE *fd = NULL;
size_t len = 0;
char buf[BUF_LEN+1] = { 0 };
FILE *fd = NULL;
char *buf = NULL;
struct blob_attr *tb[__INSTANCE_ATTR_MAX];
const char *fname = basename((char *) filename);
@ -75,26 +75,32 @@ static void test_blobmsg_procd_instance(const char *filename)
return;
}
len = fread(&buf, 1, BUF_LEN, fd);
buf = malloc(BUF_LEN+1);
if (!buf)
return;
len = fread(buf, 1, BUF_LEN, fd);
fclose(fd);
r = blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb, buf, len);
if (r)
return;
goto out;
if (!tb[INSTANCE_ATTR_COMMAND] || !tb[INSTANCE_ATTR_NICE] || !tb[INSTANCE_ATTR_STDERR])
return;
goto out;
if (!blobmsg_check_attr_list(tb[INSTANCE_ATTR_COMMAND], BLOBMSG_TYPE_STRING))
return;
goto out;
if (blobmsg_get_u32(tb[INSTANCE_ATTR_NICE]) != 19)
return;
goto out;
if (!blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
return;
goto out;
fprintf(stderr, "%s: OK\n", fname);
out:
free(buf);
}
int main(int argc, char *argv[])