giochannel: Fix incorrect use of a signed gsize when unsigned will do

The value of `wrote_bytes` will never be negative, so there’s no need to
store it in a signed type.

Add a couple of assertions to validate that it never decreases and hence
can never go negative.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-11-22 14:15:11 +00:00
parent b2dd6d9053
commit a03160adf3

View File

@ -2205,7 +2205,7 @@ g_io_channel_write_chars (GIOChannel *channel,
{
gsize count_unsigned;
GIOStatus status;
gssize wrote_bytes = 0;
gsize wrote_bytes = 0;
g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
g_return_val_if_fail (buf != NULL || count == 0, G_IO_STATUS_ERROR);
@ -2313,7 +2313,11 @@ g_io_channel_write_chars (GIOChannel *channel,
if (!channel->encoding)
{
gssize write_this = MIN (space_in_buf, count_unsigned - wrote_bytes);
gsize write_this = MIN (space_in_buf, count_unsigned - wrote_bytes);
/* g_string_append_len() takes a gssize, so dont overflow it*/
if (write_this > G_MAXSSIZE)
write_this = G_MAXSSIZE;
g_string_append_len (channel->write_buf, buf, write_this);
buf += write_this;
@ -2476,7 +2480,10 @@ reconvert:
g_warning ("Illegal sequence due to partial character "
"at the end of a previous write.");
else
wrote_bytes += from_buf_len - left_len - from_buf_old_len;
{
g_assert (from_buf_len >= left_len + from_buf_old_len);
wrote_bytes += from_buf_len - left_len - from_buf_old_len;
}
if (bytes_written)
*bytes_written = wrote_bytes;
channel->partial_write_buf[0] = '\0';