libubox/tests/test-blobmsg-parse.c
Petr Štetiar 5c0faaf4f5 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>
2020-01-20 16:54:10 +01:00

80 lines
1.5 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <libgen.h>
#include "blobmsg.h"
enum {
FOO_MESSAGE,
FOO_LIST,
FOO_TESTDATA,
__FOO_MAX
};
static const struct blobmsg_policy foo_policy[] = {
[FOO_MESSAGE] = {
.name = "message",
.type = BLOBMSG_TYPE_STRING,
},
[FOO_LIST] = {
.name = "list",
.type = BLOBMSG_TYPE_ARRAY,
},
[FOO_TESTDATA] = {
.name = "testdata",
.type = BLOBMSG_TYPE_TABLE,
},
};
static void dump_result(const char *fn, int r, const char *filename, struct blob_attr **tb)
{
fprintf(stdout, "%s: %s: %c%c%c (%d)\n", basename((char *) filename), fn,
tb[FOO_MESSAGE] ? 'M' : '.',
tb[FOO_LIST] ? 'L' : '.',
tb[FOO_TESTDATA] ? 'T' : '.',
r);
}
static void test_blobmsg(const char *filename)
{
#define BUF_LEN 256
int r = 0;
size_t len = 0;
FILE *fd = NULL;
char *buf = NULL;
struct blob_attr *tb[__FOO_MAX];
fd = fopen(filename, "r");
if (!fd) {
fprintf(stderr, "unable to open %s\n", filename);
return;
}
buf = malloc(BUF_LEN+1);
if (!buf)
return;
len = fread(buf, 1, BUF_LEN, fd);
fclose(fd);
r = blobmsg_parse(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len);
dump_result("blobmsg_parse", r, filename, tb);
r = blobmsg_parse_array(foo_policy, ARRAY_SIZE(foo_policy), tb, buf, len);
dump_result("blobmsg_parse_array", r, filename, tb);
free(buf);
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <blobmsg.bin>\n", argv[0]);
return 3;
}
test_blobmsg(argv[1]);
return 0;
}