core/main: fix setting of arguments for shutdown
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 24 Mar 2023 13:11:48 +0000 (14:11 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 24 Mar 2023 19:08:20 +0000 (19:08 +0000)
commit4688b08984cef43414a236d1c2afc5860a1703b9
treefd00a4e9246b493889f3cff9083a7214fce06fbe
parent85a336f81d9cb3b6e75e03031a8ab5d4eb682e25
core/main: fix setting of arguments for shutdown

Fixup for d2ebd50d7f9740dcf30e84efc75610af173967d2
and 6920049fad4fa39db5fec712f82f7f75b98fd4b9:
- add a comment that the last arg must be NULL and adjust the assert.
- move initialization around so that fields are declared,
  initialized, and consumed in the same order.
- move declaration of pos adjacent do declaration of command_line.
  This makes it easy to see that it was not initialized correctly.
- initialize buffers before writing the pointer into the args array.
  This makes no difference for the compiler, but it just feels "wrong"
  to do it in opposite order.

Because pos was off, we would ignore args after the timeout, and also
overwrite the buffer if enough args were used.

I think this is case shows clearly that declaring all variables at the
top of the function, with some initialized and other not, is very
error-prone. The compiler has no issue with declaring variables whereever,
and we should take advantage of this to make it keep declaration,
initialization, and use close. (Within reason of course.)
src/core/main.c