Monday 22 April 2013

Fun with Null Terminated Character Arrays

The function svn_io_create_custom_diff_cmd(...) that I wrote returns a null-terminated character array (ntca) which
svn_io_run_external_diff(...) passes to

svn_io_run_cmd(...) which passes it to

svn_io_start_cmd3(...) where this loop:

for (num_args = 0; args[num_args]; num_args++)
       ;

relies on the NULL as a sentinel, before copying and utf converting it into the new ntca it then passes into apr_proc_create().

This arrangement presented me with quite some difficulty because it failed at random points and made it appear as if my function was correct, but that the diff programs themselves were randomly failing with colourful error messages.

Unfortunately, I was completely innocent of the idea that this NULL terminator would be an implicit conditional 3 functions ahead.

I mistakenly offset the NULL by one and ... I got a very good (and thorough) demonstration of how droll a broken pointer can be ;-D