diff options
author | David Robillard <d@drobilla.net> | 2024-12-11 19:08:06 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2024-12-11 19:39:22 -0500 |
commit | 8bd6f662b5cf5111985daad42b21b9da37ab6a44 (patch) | |
tree | ef00b3467bb1c8b8bbba006a6a7f72b390d6692f /test | |
parent | 35354abb37202f99bec2d65e437dd7e2f8ffbc9d (diff) | |
download | lilv-8bd6f662b5cf5111985daad42b21b9da37ab6a44.tar.gz lilv-8bd6f662b5cf5111985daad42b21b9da37ab6a44.tar.bz2 lilv-8bd6f662b5cf5111985daad42b21b9da37ab6a44.zip |
Avoid potential null dereferences and use of unterminated string
Diffstat (limited to 'test')
-rw-r--r-- | test/test_plugin.lv2/test_plugin.c | 24 | ||||
-rw-r--r-- | test/test_util.c | 2 |
2 files changed, 16 insertions, 10 deletions
diff --git a/test/test_plugin.lv2/test_plugin.c b/test/test_plugin.lv2/test_plugin.c index 572443b..c8851d4 100644 --- a/test/test_plugin.lv2/test_plugin.c +++ b/test/test_plugin.lv2/test_plugin.c @@ -301,8 +301,10 @@ save(LV2_Handle instance, if (make_path) { char* spath = make_path->path(make_path->handle, "save"); FILE* sfile = fopen(spath, "w"); - fprintf(sfile, "save"); - fclose(sfile); + if (sfile) { + fprintf(sfile, "save"); + fclose(sfile); + } apath = map_path->abstract_path(map_path->handle, spath); store(callback_data, @@ -367,14 +369,16 @@ restore(LV2_Handle instance, } if (apath) { - char* path = map_path->absolute_path(map_path->handle, apath); - FILE* f = fopen(path, "r"); - char str[8]; - size_t n_read = fread(str, 1, sizeof(str), f); - fclose(f); - if (!!strncmp(str, "Hello\n", n_read)) { - fprintf( - stderr, "error: Restored bad file contents `%s' != `Hello'\n", str); + char* path = map_path->absolute_path(map_path->handle, apath); + FILE* f = fopen(path, "r"); + if (f) { + char str[8] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'}; + size_t n_read = fread(str, 1, 6, f); + fclose(f); + + if (!!strncmp(str, "Hello\n", n_read)) { + fprintf(stderr, "error: Restored bad file `%s' != `Hello'\n", str); + } } free_path->free_path(free_path->handle, path); } diff --git a/test/test_util.c b/test/test_util.c index 22edbbe..3063aa7 100644 --- a/test/test_util.c +++ b/test/test_util.c @@ -22,6 +22,8 @@ main(void) FILE* const fa = fopen(a_path, "w"); FILE* const fb = fopen(b_path, "w"); + assert(fa); + assert(fb); fprintf(fa, "AA\n"); fprintf(fb, "AB\n"); fclose(fb); |