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 f40d31b5cc)
This commit is contained in:
Simon Tatham 2020-01-29 06:13:41 +00:00
Родитель 8c227b0cc0
Коммит 5b09e4c250
2 изменённых файлов: 22 добавлений и 18 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -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));
}