From b0a42f20581f7131ee10756571e20ee2a52d827f Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Tue, 18 Dec 2001 18:46:41 +0000 Subject: [PATCH] use g_try_malloc and return error on not-enough-memory 2001-12-16 Havoc Pennington * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and return error on not-enough-memory (get_contents_stdio): ditto --- ChangeLog | 6 ++ ChangeLog.pre-2-0 | 6 ++ ChangeLog.pre-2-10 | 6 ++ ChangeLog.pre-2-12 | 6 ++ ChangeLog.pre-2-2 | 6 ++ ChangeLog.pre-2-4 | 6 ++ ChangeLog.pre-2-6 | 6 ++ ChangeLog.pre-2-8 | 6 ++ .../gobject/tmpl/gobject-unused.sgml | 56 ++++++++++++++ docs/reference/gobject/tmpl/gtype.sgml | 56 -------------- glib/gfileutils.c | 74 +++++++++++++++---- 11 files changed, 162 insertions(+), 72 deletions(-) diff --git a/ChangeLog b/ChangeLog index 18d66c191..b00cca904 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2001-12-16 Havoc Pennington + + * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and + return error on not-enough-memory + (get_contents_stdio): ditto + 2001-12-17 Matthias Clasen * glib/gspawn.c, glib/gspawn-win32.c: Use , not . diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 18d66c191..b00cca904 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,9 @@ +2001-12-16 Havoc Pennington + + * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and + return error on not-enough-memory + (get_contents_stdio): ditto + 2001-12-17 Matthias Clasen * glib/gspawn.c, glib/gspawn-win32.c: Use , not . diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 18d66c191..b00cca904 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,9 @@ +2001-12-16 Havoc Pennington + + * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and + return error on not-enough-memory + (get_contents_stdio): ditto + 2001-12-17 Matthias Clasen * glib/gspawn.c, glib/gspawn-win32.c: Use , not . diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 18d66c191..b00cca904 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,9 @@ +2001-12-16 Havoc Pennington + + * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and + return error on not-enough-memory + (get_contents_stdio): ditto + 2001-12-17 Matthias Clasen * glib/gspawn.c, glib/gspawn-win32.c: Use , not . diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 18d66c191..b00cca904 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,9 @@ +2001-12-16 Havoc Pennington + + * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and + return error on not-enough-memory + (get_contents_stdio): ditto + 2001-12-17 Matthias Clasen * glib/gspawn.c, glib/gspawn-win32.c: Use , not . diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 18d66c191..b00cca904 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,9 @@ +2001-12-16 Havoc Pennington + + * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and + return error on not-enough-memory + (get_contents_stdio): ditto + 2001-12-17 Matthias Clasen * glib/gspawn.c, glib/gspawn-win32.c: Use , not . diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 18d66c191..b00cca904 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,9 @@ +2001-12-16 Havoc Pennington + + * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and + return error on not-enough-memory + (get_contents_stdio): ditto + 2001-12-17 Matthias Clasen * glib/gspawn.c, glib/gspawn-win32.c: Use , not . diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 18d66c191..b00cca904 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,9 @@ +2001-12-16 Havoc Pennington + + * glib/gfileutils.c (get_contents_regfile): use g_try_malloc and + return error on not-enough-memory + (get_contents_stdio): ditto + 2001-12-17 Matthias Clasen * glib/gspawn.c, glib/gspawn-win32.c: Use , not . diff --git a/docs/reference/gobject/tmpl/gobject-unused.sgml b/docs/reference/gobject/tmpl/gobject-unused.sgml index 70a7e1e14..b04f4a89e 100644 --- a/docs/reference/gobject/tmpl/gobject-unused.sgml +++ b/docs/reference/gobject/tmpl/gobject-unused.sgml @@ -46,6 +46,62 @@ Parameter Specifications @parent_instance: + + +The predefined identifiers of the reserved fundamental types. + + +@G_TYPE_INVALID: Usually a return value indicating an error. +@G_TYPE_NONE: A synonym for the "void" type in C. +@G_TYPE_INTERFACE: Root type of all interface types. +@G_TYPE_CHAR: Identifier for the built-in type "gchar". +@G_TYPE_UCHAR: Identifier for the built-in type "guchar". +@G_TYPE_BOOLEAN: Identifier for the built-in type "gboolean". +@G_TYPE_INT: Identifier for the built-in type "gint". +@G_TYPE_UINT: Identifier for the built-in type "guint". +@G_TYPE_LONG: Identifier for the built-in type "glong". +@G_TYPE_ULONG: Identifier for the built-in type "gulong". +@G_TYPE_INT64: +@G_TYPE_UINT64: +@G_TYPE_ENUM: Identifier for the "#GEnum" type. +@G_TYPE_FLAGS: Identifier for the "#GFlags" type. +@G_TYPE_FLOAT: Identifier for the built-in type "gfloat". +@G_TYPE_DOUBLE: Identifier for the built-in type "gdouble". +@G_TYPE_STRING: Identifier for a pointer to a null-terminated string "gchar*". +@G_TYPE_POINTER: Identifier for anonymous pointers "void*". +@G_TYPE_BOXED: Identifier for the "#GBoxed" type. +@G_TYPE_PARAM: Identifier for the "#GParam" type. +@G_TYPE_OBJECT: Identifier for the "#GObject" type. +@G_TYPE_RESERVED_BSE_FIRST: First fundamental type ID reserved for BSE. +@G_TYPE_RESERVED_BSE_LAST: Last fundamental type ID reserved for BSE. +@G_TYPE_RESERVED_LAST_FUNDAMENTAL: +@G_TYPE_CONSTANT_TYPES: +@G_TYPE_CLOSURE: +@G_TYPE_VALUE: +@G_TYPE_VALUE_ARRAY: +@G_TYPE_GSTRING: +@G_TYPE_PARAM_CHAR: +@G_TYPE_PARAM_UCHAR: +@G_TYPE_PARAM_BOOLEAN: +@G_TYPE_PARAM_INT: +@G_TYPE_PARAM_UINT: +@G_TYPE_PARAM_LONG: +@G_TYPE_PARAM_ULONG: +@G_TYPE_PARAM_INT64: +@G_TYPE_PARAM_UINT64: +@G_TYPE_PARAM_UNICHAR: +@G_TYPE_PARAM_ENUM: +@G_TYPE_PARAM_FLAGS: +@G_TYPE_PARAM_FLOAT: +@G_TYPE_PARAM_DOUBLE: +@G_TYPE_PARAM_STRING: +@G_TYPE_PARAM_PARAM: +@G_TYPE_PARAM_BOXED: +@G_TYPE_PARAM_POINTER: +@G_TYPE_PARAM_VALUE_ARRAY: +@G_TYPE_PARAM_CLOSURE: +@G_TYPE_PARAM_OBJECT: + diff --git a/docs/reference/gobject/tmpl/gtype.sgml b/docs/reference/gobject/tmpl/gtype.sgml index fe6ce1ca3..9e85621c0 100644 --- a/docs/reference/gobject/tmpl/gtype.sgml +++ b/docs/reference/gobject/tmpl/gtype.sgml @@ -161,62 +161,6 @@ interfaces and C++ classes containing only pure virtual functions. @type: A #GType value. - - -The predefined identifiers of the reserved fundamental types. - - -@G_TYPE_INVALID: Usually a return value indicating an error. -@G_TYPE_NONE: A synonym for the "void" type in C. -@G_TYPE_INTERFACE: Root type of all interface types. -@G_TYPE_CHAR: Identifier for the built-in type "gchar". -@G_TYPE_UCHAR: Identifier for the built-in type "guchar". -@G_TYPE_BOOLEAN: Identifier for the built-in type "gboolean". -@G_TYPE_INT: Identifier for the built-in type "gint". -@G_TYPE_UINT: Identifier for the built-in type "guint". -@G_TYPE_LONG: Identifier for the built-in type "glong". -@G_TYPE_ULONG: Identifier for the built-in type "gulong". -@G_TYPE_INT64: -@G_TYPE_UINT64: -@G_TYPE_ENUM: Identifier for the "#GEnum" type. -@G_TYPE_FLAGS: Identifier for the "#GFlags" type. -@G_TYPE_FLOAT: Identifier for the built-in type "gfloat". -@G_TYPE_DOUBLE: Identifier for the built-in type "gdouble". -@G_TYPE_STRING: Identifier for a pointer to a null-terminated string "gchar*". -@G_TYPE_POINTER: Identifier for anonymous pointers "void*". -@G_TYPE_BOXED: Identifier for the "#GBoxed" type. -@G_TYPE_PARAM: Identifier for the "#GParam" type. -@G_TYPE_OBJECT: Identifier for the "#GObject" type. -@G_TYPE_RESERVED_BSE_FIRST: First fundamental type ID reserved for BSE. -@G_TYPE_RESERVED_BSE_LAST: Last fundamental type ID reserved for BSE. -@G_TYPE_RESERVED_LAST_FUNDAMENTAL: -@G_TYPE_CONSTANT_TYPES: -@G_TYPE_CLOSURE: -@G_TYPE_VALUE: -@G_TYPE_VALUE_ARRAY: -@G_TYPE_GSTRING: -@G_TYPE_PARAM_CHAR: -@G_TYPE_PARAM_UCHAR: -@G_TYPE_PARAM_BOOLEAN: -@G_TYPE_PARAM_INT: -@G_TYPE_PARAM_UINT: -@G_TYPE_PARAM_LONG: -@G_TYPE_PARAM_ULONG: -@G_TYPE_PARAM_INT64: -@G_TYPE_PARAM_UINT64: -@G_TYPE_PARAM_UNICHAR: -@G_TYPE_PARAM_ENUM: -@G_TYPE_PARAM_FLAGS: -@G_TYPE_PARAM_FLOAT: -@G_TYPE_PARAM_DOUBLE: -@G_TYPE_PARAM_STRING: -@G_TYPE_PARAM_PARAM: -@G_TYPE_PARAM_BOXED: -@G_TYPE_PARAM_POINTER: -@G_TYPE_PARAM_VALUE_ARRAY: -@G_TYPE_PARAM_CLOSURE: -@G_TYPE_PARAM_OBJECT: - An opaque structure used as the base of all interface types. diff --git a/glib/gfileutils.c b/glib/gfileutils.c index 1d7a3907f..cb287bec8 100644 --- a/glib/gfileutils.c +++ b/glib/gfileutils.c @@ -291,15 +291,37 @@ get_contents_stdio (const gchar *filename, { gchar buf[2048]; size_t bytes; - GString *str; - - g_assert (f != NULL); + char *str; + size_t total_bytes; + size_t total_allocated; - str = g_string_new (""); + g_assert (f != NULL); + +#define STARTING_ALLOC 64 + + total_bytes = 0; + total_allocated = STARTING_ALLOC; + str = g_malloc (STARTING_ALLOC); while (!feof (f)) { bytes = fread (buf, 1, 2048, f); + + while ((total_bytes + bytes + 1) > total_allocated) + { + total_allocated *= 2; + str = g_try_realloc (str, total_allocated); + + if (str == NULL) + { + g_set_error (error, + G_FILE_ERROR, + G_FILE_ERROR_NOMEM, + _("Could not allocate %lu bytes to read file \"%s\""), + (gulong) total_allocated, filename); + goto error; + } + } if (ferror (f)) { @@ -309,23 +331,30 @@ get_contents_stdio (const gchar *filename, _("Error reading file '%s': %s"), filename, strerror (errno)); - g_string_free (str, TRUE); - fclose (f); - - return FALSE; + goto error; } - g_string_append_len (str, buf, bytes); + memcpy (str + total_bytes, buf, bytes); + total_bytes += bytes; } fclose (f); - if (length) - *length = str->len; + str[total_bytes] = '\0'; - *contents = g_string_free (str, FALSE); + if (length) + *length = total_bytes; + + *contents = str; + + return TRUE; - return TRUE; + error: + + g_free (str); + fclose (f); + + return FALSE; } #ifndef G_OS_WIN32 @@ -341,11 +370,24 @@ get_contents_regfile (const gchar *filename, gchar *buf; size_t bytes_read; size_t size; - + size_t alloc_size; + size = stat_buf->st_size; - buf = g_new (gchar, size + 1); - + alloc_size = size + 1; + buf = g_try_malloc (alloc_size); + + if (buf == NULL) + { + g_set_error (error, + G_FILE_ERROR, + G_FILE_ERROR_NOMEM, + _("Could not allocate %lu bytes to read file \"%s\""), + (gulong) alloc_size, filename); + + return FALSE; + } + bytes_read = 0; while (bytes_read < size) {