From 6d77541080f463b1c4743e834e41f31169fdf60a Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 29 Jan 2022 18:11:06 +0000 Subject: [PATCH] bidi_test: minor memory fixes. Spotted by Coverity: if you _just_ gave a filename to bidi_test, without any previous argument that set testfn to something other than NULL, the program would crash rather than giving an error message. (It's only a test program, but test programs you only run once in a blue moon are the ones that _most_ need to explain their command-line syntax to you carefully, because you've forgotten it since last time you used them!) Also, conditionalised a memcpy on the size not being 0, because it's illegal to pass a null pointer to memcpy _even_ if size==0. (That would only happen with a test case containing a zero-length string, but whatever.) --- terminal/bidi_test.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/terminal/bidi_test.c b/terminal/bidi_test.c index 0b63029f..1acd1d68 100644 --- a/terminal/bidi_test.c +++ b/terminal/bidi_test.c @@ -44,7 +44,8 @@ static void run_test(const char *filename, unsigned lineno, { size_t bcs_orig_len = bcs_len; bidi_char *bcs_orig = snewn(bcs_orig_len, bidi_char); - memcpy(bcs_orig, bcs, bcs_orig_len * sizeof(bidi_char)); + if (bcs_orig_len) + memcpy(bcs_orig, bcs, bcs_orig_len * sizeof(bidi_char)); bcs_len = do_bidi_test(ctx, bcs, bcs_len, override); @@ -335,6 +336,12 @@ int main(int argc, char **argv) } else { const char *filename = arg; + if (!testfn) { + fprintf(stderr, "no mode argument provided before filename " + "'%s'\n", filename); + return 1; + } + if (!strcmp(filename, "-")) { testfn("", stdin); } else {