use g_try_malloc and return error on not-enough-memory
authorHavoc Pennington <hp@pobox.com>
Tue, 18 Dec 2001 18:46:41 +0000 (18:46 +0000)
committerHavoc Pennington <hp@src.gnome.org>
Tue, 18 Dec 2001 18:46:41 +0000 (18:46 +0000)
2001-12-16  Havoc Pennington  <hp@pobox.com>

* glib/gfileutils.c (get_contents_regfile): use g_try_malloc and
return error on not-enough-memory
(get_contents_stdio): ditto

ChangeLog
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
docs/reference/gobject/tmpl/gobject-unused.sgml
docs/reference/gobject/tmpl/gtype.sgml
glib/gfileutils.c

index 18d66c1..b00cca9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2001-12-16  Havoc Pennington  <hp@pobox.com>
+
+       * 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  <matthiasc@poet.de>
 
        * glib/gspawn.c, glib/gspawn-win32.c: Use <envar>, not <envvar>.
index 18d66c1..b00cca9 100644 (file)
@@ -1,3 +1,9 @@
+2001-12-16  Havoc Pennington  <hp@pobox.com>
+
+       * 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  <matthiasc@poet.de>
 
        * glib/gspawn.c, glib/gspawn-win32.c: Use <envar>, not <envvar>.
index 18d66c1..b00cca9 100644 (file)
@@ -1,3 +1,9 @@
+2001-12-16  Havoc Pennington  <hp@pobox.com>
+
+       * 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  <matthiasc@poet.de>
 
        * glib/gspawn.c, glib/gspawn-win32.c: Use <envar>, not <envvar>.
index 18d66c1..b00cca9 100644 (file)
@@ -1,3 +1,9 @@
+2001-12-16  Havoc Pennington  <hp@pobox.com>
+
+       * 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  <matthiasc@poet.de>
 
        * glib/gspawn.c, glib/gspawn-win32.c: Use <envar>, not <envvar>.
index 18d66c1..b00cca9 100644 (file)
@@ -1,3 +1,9 @@
+2001-12-16  Havoc Pennington  <hp@pobox.com>
+
+       * 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  <matthiasc@poet.de>
 
        * glib/gspawn.c, glib/gspawn-win32.c: Use <envar>, not <envvar>.
index 18d66c1..b00cca9 100644 (file)
@@ -1,3 +1,9 @@
+2001-12-16  Havoc Pennington  <hp@pobox.com>
+
+       * 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  <matthiasc@poet.de>
 
        * glib/gspawn.c, glib/gspawn-win32.c: Use <envar>, not <envvar>.
index 18d66c1..b00cca9 100644 (file)
@@ -1,3 +1,9 @@
+2001-12-16  Havoc Pennington  <hp@pobox.com>
+
+       * 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  <matthiasc@poet.de>
 
        * glib/gspawn.c, glib/gspawn-win32.c: Use <envar>, not <envvar>.
index 18d66c1..b00cca9 100644 (file)
@@ -1,3 +1,9 @@
+2001-12-16  Havoc Pennington  <hp@pobox.com>
+
+       * 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  <matthiasc@poet.de>
 
        * glib/gspawn.c, glib/gspawn-win32.c: Use <envar>, not <envvar>.
index 70a7e1e..b04f4a8 100644 (file)
@@ -46,6 +46,62 @@ Parameter Specifications
 
 @parent_instance: 
 
+<!-- ##### ENUM GTypeFundamentals ##### -->
+<para>
+The predefined identifiers of the reserved fundamental types.
+</para>
+
+@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: 
+
 <!-- ##### MACRO G_IS_PARAM_SPEC_CLOSURE ##### -->
 <para>
 
index fe6ce1c..9e85621 100644 (file)
@@ -161,62 +161,6 @@ interfaces and C++ classes containing only pure virtual functions.
 @type: A #GType value.
 
 
-<!-- ##### ENUM GTypeFundamentals ##### -->
-<para>
-The predefined identifiers of the reserved fundamental types.
-</para>
-
-@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: 
-
 <!-- ##### STRUCT GTypeInterface ##### -->
 <para>
 An opaque structure used as the base of all interface types.
index 1d7a390..cb287be 100644 (file)
@@ -291,15 +291,37 @@ get_contents_stdio (const gchar *filename,
 {
   gchar buf[2048];
   size_t bytes;
-  GString *str;
-
+  char *str;
+  size_t total_bytes;
+  size_t total_allocated;
+  
   g_assert (f != NULL);
+
+#define STARTING_ALLOC 64
   
-  str = g_string_new ("");
+  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);
 
+  str[total_bytes] = '\0';
+  
   if (length)
-    *length = str->len;
+    *length = total_bytes;
+  
+  *contents = str;
   
-  *contents = g_string_free (str, FALSE);
+  return TRUE;
+
+ error:
 
-  return TRUE;  
+  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)
     {