From 5b09e4c250d2cf77f5fecbaa6284fd8a100984f4 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 29 Jan 2020 06:13:41 +0000 Subject: [PATCH] Fix technical-UB uses of the preprocessor. A recent test-compile at high warning level points out that if you define a macro with a ... at the end of the parameter list, then every call should at least include the comma before the variadic part. That is, if you #define MACRO(x,y,...) then you shouldn't call MACRO(1,2) with no comma after the 2. But that's what I had done in one of my definitions of FUNC0 in the fiddly testcrypt system. In a similar vein, it's a mistake to use the preprocessor 'defined' operator when it's expanded from another macro. Adjusted the setup of BB_OK in mpint_i.h to avoid doing that. (Neither of these has yet caused a problem in any real compile, but best to fix them before they do.) (cherry picked from commit f40d31b5ccb6b771d516916294cf75eac3f15ed5) --- mpint_i.h | 6 +++++- testcrypt.c | 34 +++++++++++++++++----------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/mpint_i.h b/mpint_i.h index 70fdc6e1..23f6dc9d 100644 --- a/mpint_i.h +++ b/mpint_i.h @@ -59,7 +59,11 @@ /* You can lower the BignumInt size by defining BIGNUM_OVERRIDE on the * command line to be your chosen max value of BIGNUM_INT_BITS_BITS */ -#define BB_OK(b) (!defined BIGNUM_OVERRIDE || BIGNUM_OVERRIDE >= b) +#if defined BIGNUM_OVERRIDE +#define BB_OK(b) ((b) <= BIGNUM_OVERRIDE) +#else +#define BB_OK(b) (1) +#endif #if defined __SIZEOF_INT128__ && BB_OK(6) diff --git a/testcrypt.c b/testcrypt.c index aa41a7a5..fde77328 100644 --- a/testcrypt.c +++ b/testcrypt.c @@ -1001,30 +1001,28 @@ static void process_line(BinarySource *in, strbuf *out) { ptrlen id = get_word(in); -#define DISPATCH_COMMAND(cmd) \ - if (ptrlen_eq_string(id, #cmd)) { \ - handle_##cmd(in, out); \ - return; \ - } +#define DISPATCH_INTERNAL(cmdname, handler) do { \ + if (ptrlen_eq_string(id, cmdname)) { \ + handler(in, out); \ + return; \ + } \ + } while (0) + +#define DISPATCH_COMMAND(cmd) DISPATCH_INTERNAL(#cmd, handle_##cmd) DISPATCH_COMMAND(hello); DISPATCH_COMMAND(free); DISPATCH_COMMAND(newstring); DISPATCH_COMMAND(getstring); DISPATCH_COMMAND(mp_literal); DISPATCH_COMMAND(mp_dump); +#undef DISPATCH_COMMAND -#define FUNC(rettype, function, ...) \ - if (ptrlen_eq_string(id, #function)) { \ - handle_##function(in, out); \ - return; \ - } - -#define FUNC0 FUNC -#define FUNC1 FUNC -#define FUNC2 FUNC -#define FUNC3 FUNC -#define FUNC4 FUNC -#define FUNC5 FUNC +#define FUNC0(ret,func) DISPATCH_INTERNAL(#func, handle_##func); +#define FUNC1(ret,func,x) DISPATCH_INTERNAL(#func, handle_##func); +#define FUNC2(ret,func,x,y) DISPATCH_INTERNAL(#func, handle_##func); +#define FUNC3(ret,func,x,y,z) DISPATCH_INTERNAL(#func, handle_##func); +#define FUNC4(ret,func,x,y,z,v) DISPATCH_INTERNAL(#func, handle_##func); +#define FUNC5(ret,func,x,y,z,v,w) DISPATCH_INTERNAL(#func, handle_##func); #include "testcrypt.h" @@ -1035,6 +1033,8 @@ static void process_line(BinarySource *in, strbuf *out) #undef FUNC1 #undef FUNC0 +#undef DISPATCH_INTERNAL + fatal_error("command '%.*s': unrecognised", PTRLEN_PRINTF(id)); }