After taking a break from this challenge and coming back a day later, the error went away. I was able to complete the rest of the stages, but this error is now intermittently coming back and I’m not sure what is causing it.
I ran the program through Valgrind and it helped me diagnose the issue and solve it. I’ll give a quick summary in case anyone else has a similar issue.
==9875== Invalid write of size 8
==9875== at 0x10AA26: add_cmd_args (cmd_arg_parser.c:71)
==9875== by 0x10D815: main (main.c:33)
==9875== Address 0x4b0a0f0 is 0 bytes after a block of size 80 alloc'd
==9875== at 0x484D953: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==9875== by 0x10A74E: create_args_obj (cmd_arg_parser.c:16)
==9875== by 0x10D7B2: main (main.c:25)
I created a struct to hold the arguments I parsed from the command-line (create_args_obj), but I set the initial size of the args array to hold 10 arguments:
In add_cmd_args, I wasn’t doing a bounds-check to make sure that ao→size wasn’t about to reach the capacity, and if so, resize ao→args. As a result, when my program tried parsing the following line from the test:
it was parsing more than 10 arguments without resizing ao→args and would corrupt memory by writing out-of-bounds. I added the following bounds-check to add_cmd_args: