g_io_write_chars calls abort when given a null byte as input
Submitted by David Shea
Link to original bug (#750877)
Description
Created attachment 305178 Allow 0 bytes in fast_validate_len
I know that this is a bug in my program, because I'm not calling g_io_channel_set_encoding(channel, NULL, ...) before sending binary to a GIOChannel, but it should just return an error, not abort.
Here's a reproducer:
from gi.repository import GLib channel = GLib.IOChannel.new_file("whatever.txt", "w") channel.write_chars('\0', 1)
The problem is that g_io_channel_write_chars is calling g_utf8_validate to determine if the string is valid utf-8, and then calling g_utf8_get_char_validated to determine what kind of error it is. g_utf8_validate returns FALSE for the 0 byte, which is technically wrong, and then g_utf8_get_char_validated returns 0, the character, which is technically right, and since the 0 isn't an error return it causes issues in the switch statement trying to determine the error type.
I think the right thing to do would be to have fast_validate_len actually trust the len instead of looking for and stopping on 0 bytes, but I don't know what kind of effect that would have on other code. Attached a patch.
Patch 305178, "Allow 0 bytes in fast_validate_len":
0001-Allow-0-bytes-in-fast_validate_len.patch
Version: 2.45.x