From e9e4d52de8a8424a11f61512a75902ab06f29375 Mon Sep 17 00:00:00 2001 From: CCode Date: Mon, 16 Jan 2023 13:08:42 +0000 Subject: [PATCH] gfileutils: Use 'write' with 'count' <= max value of its return type Limit `count` so that `write` can properly report the number of written bytes. Limits: - POSIX: `SSIZE_MAX` - Windows: `INT_MAX` Fixes: #2883 --- glib/gfileutils.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/glib/gfileutils.c b/glib/gfileutils.c index 44c22f3ee..131484901 100644 --- a/glib/gfileutils.c +++ b/glib/gfileutils.c @@ -1143,8 +1143,13 @@ write_to_file (const gchar *contents, { gssize s; - s = write (fd, contents, MIN (length, G_MAXSIZE)); - +#ifdef G_OS_WIN32 + /* 'write' on windows uses int types, so limit count to G_MAXINT */ + s = write (fd, contents, MIN (length, (gsize) G_MAXINT)); +#else + /* Limit count to G_MAXSSIZE to fit into the return value. */ + s = write (fd, contents, MIN (length, (gsize) G_MAXSSIZE)); +#endif if (s < 0) { int saved_errno = errno;