diff --git a/glib/gbase64.c b/glib/gbase64.c index 2ea4a4ef44174ef554777c602be782ae7535b7fc..0141b3b072d06a3d545bb7215ce106bed2094363 100644 --- a/glib/gbase64.c +++ b/glib/gbase64.c @@ -240,8 +240,10 @@ g_base64_encode (const guchar *data, gsize len) { gchar *out; - gint state = 0, outlen; + gint state = 0; gint save = 0; + gsize outlen; + gsize allocsize; g_return_val_if_fail (data != NULL || len == 0, NULL); @@ -249,10 +251,15 @@ g_base64_encode (const guchar *data, +1 is needed for trailing \0, also check for unlikely integer overflow */ g_return_val_if_fail (len < ((G_MAXSIZE - 1) / 4 - 1) * 3, NULL); - out = g_malloc ((len / 3 + 1) * 4 + 1); + allocsize = (len / 3 + 1) * 4 + 1; + out = g_malloc (allocsize); outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save); + g_assert (outlen <= allocsize); + outlen += g_base64_encode_close (FALSE, out + outlen, &state, &save); + g_assert (outlen <= allocsize); + out[outlen] = '\0'; return (gchar *) out;