Re: [hatari-devel] Patch to fix crash saving midi preferences on Mac |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/hatari-devel Archives
]
- To: hatari-devel@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [hatari-devel] Patch to fix crash saving midi preferences on Mac
- From: Thomas Huth <th.huth@xxxxxxxxx>
- Date: Sat, 28 May 2022 07:19:51 +0000
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1653722395; bh=nFpjk/HEJr3TbqECpnxHDPcIjRZBc1livSjij0hpl60=; h=Date:From:To:Subject:From; b=d5qzEPWzi9tRakyzN4E/k2PVscYHwblz6tdq9x1q68QFRijYuG5MUvPDIMc518zA5 QYpz8p2y50Yxlv+9a10oqL0vEdyGQQuhqWuah3fJknW5Efks9D/XL8j/6uzVFCvuTc btt+vGc+xAAYIWa7GU5sUv6Et3eeM6/S4aF4WP+QKpi+eO1tNjWafbVmjy6toVrykz 4rNCJEe7xZM/DpDw9rzb0AiZ0fnpQi5F4ebinP0mNAcL+WFNIbGGhzpGkhDxgp328+ E5iR25zOHYoztzEvYvth0gegZA5FjZz0wFk5BgNUTSh5kEbP7sYC69n6eaFXxOj4jx 0kA4gwAIMxlpg==
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