xiaoxiang781216 commented on pull request #1108: URL: https://github.com/apache/incubator-nuttx-apps/pull/1108#issuecomment-1081792696
> > > I just briefly searched in the code and see that `optind++;` is used in many places. Taking into account that `#define optind (*(getoptindp()))` this becomes a useless and all the code that is relying on `optind` after `optind++` is executed wrongly. > > > > > > `optind++` is good usage to access the argument after option. The problem here is that quickjs parse the argument by self and doesn't use getopt at all, but the local variable definition conflict with optind unintentionally. > > @xiaoxiang781216 I'm talking not about the problem here. The author solves it by renaming of a variable. The case is that in Linux (https://linux.die.net/man/3/optind) the `optind` is `int optind` and referenced via `extern int optind`, but in NuttX it is `FAR int *getoptindp(void); /* Index into argv */` and `#define optind (*(getoptindp()))` so `optind++;` does not increment the index holder. Please try to run a small example locally > > ``` > #include <stdio.h> > > #define optind (*(getoptindp())) > > int *getoptindp(void) > { > static int a; > return &a; > } > > int main() > { > printf("Test: %d, %d", optind++, optind); > > return 0; > } > ``` > > and you will see that zero is printed two times. But your test isn't good since compiler may evaluate the last argument first(compiler can evaluate the expression in any order). I change your printf to this: ``` printf("Test: %d, %d\n", optind++, optind++); ``` The output show that the last argument evaluate first: ``` Test: 1, 0 ``` But of course, it may change in your environment since the spec say this is an undefined behavior and anything could happen. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
