log-writes: Handle unrecognized options to prevent segfault
authorQu Wenruo <wqu@suse.com>
Thu, 8 Aug 2019 07:29:40 +0000 (15:29 +0800)
committerEryu Guan <guaneryu@gmail.com>
Sun, 11 Aug 2019 13:34:23 +0000 (21:34 +0800)
commit1592be34938615e6ef4b1e1355014d7ed1f8be90
tree8098e7e283299c744e22497013a8c19d48228589
parentcda9817fbbe4f9127eca29717ce9027faf708296
log-writes: Handle unrecognized options to prevent segfault

[BUG]
When using --help parameter (unrecognized) after valid --log/--replay,
log-writes just crashes:
  Starting program: replay-log --log /dev/test/test  --replay /dev/test/scratch1 --help
  /home/adam/xfstests-dev/src/log-writes/replay-log: unrecognized option '--help'

  Program received signal SIGSEGV, Segmentation fault.
  0x00007ffff7f5cc55 in __strlen_avx2 () from /usr/lib/libc.so.6
  (gdb) bt
  #0  0x00007ffff7f5cc55 in __strlen_avx2 () from /usr/lib/libc.so.6
  #1  0x00007ffff7e89363 in strdup () from /usr/lib/libc.so.6
  #2  0x00005555555554ac in main (argc=6, argv=0x7fffffffea78)
      at replay-log.c:219

[CAUSE]
We didn't check return value from getopt_long() for unrecognized
parameter, thus we reuse the old opt_index, and if that option needs an
parameter, we will access optarg which can be NULL and cause segfault.

[FIX]
Check return value from getopt_long() for '?' to handle unrecognized
options correctly.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
src/log-writes/replay-log.c