Re: [hatari-devel] Patch to fix crash saving midi preferences on Mac

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


Am Fri, 27 May 2022 23:19:34 +0100
schrieb Chris Jenkins <cdpjenkins@xxxxxxxxx>:
....
> It turns out that the code is supplying the wrong size to strncpy; it
> supplies FILENAME_MAX (which is 1024 on my Mac) whereas the length of the
> field is MAX_MIDI_PORT_NAME (256).
....
> Note that I didn't expect this to crash, given that the text that is being
> copied in this case is "Off" which isn't anywhere near 256 characters long.

You missed the "hidden feature" of the strncpy function, citing the (Linux)
man page:

"If the length of src is less than n, strncpy() writes additional null
bytes to dest to ensure that a total of n bytes are written."

That means strncpy always fills the whole destination buffer - using \0 if
the source string is not long enough.

That's one more reason to rather use strlcpy instead of strncpy in most
cases.

 Thomas



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/