Merge branch 'backport-4935-file-attribute-maths-glib-2-86' into 'glib-2-86'

Backport !4935 “gfileattribute: Fix integer overflow calculating escaping for byte strings” to glib-2-86

See merge request GNOME/glib!4936
This commit is contained in:
Marco Trevisan
2025-12-06 01:01:37 +01:00

View File

@@ -22,6 +22,7 @@
#include "config.h" #include "config.h"
#include <stdint.h>
#include <string.h> #include <string.h>
#include "gfileattribute.h" #include "gfileattribute.h"
@@ -166,11 +167,12 @@ valid_char (char c)
return c >= 32 && c <= 126 && c != '\\'; return c >= 32 && c <= 126 && c != '\\';
} }
/* Returns NULL on error */
static char * static char *
escape_byte_string (const char *str) escape_byte_string (const char *str)
{ {
size_t i, len; size_t i, len;
int num_invalid; size_t num_invalid;
char *escaped_val, *p; char *escaped_val, *p;
unsigned char c; unsigned char c;
const char hex_digits[] = "0123456789abcdef"; const char hex_digits[] = "0123456789abcdef";
@@ -188,6 +190,11 @@ escape_byte_string (const char *str)
return g_strdup (str); return g_strdup (str);
else else
{ {
/* Check for overflow. We want to check the inequality:
* !(len + num_invalid * 3 + 1 > SIZE_MAX) */
if (num_invalid >= (SIZE_MAX - len) / 3)
return NULL;
escaped_val = g_malloc (len + num_invalid * 3 + 1); escaped_val = g_malloc (len + num_invalid * 3 + 1);
p = escaped_val; p = escaped_val;