Author: rolf
Date: 2007-10-31 14:17:34 -0500 (Wed, 31 Oct 2007)
New Revision: 88604

Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/asf/ChangeLog
   trunk/moon/src/asf/asf-generated.cpp
   trunk/moon/src/asf/asf-generated.h
   trunk/moon/src/asf/asf-structures.cpp
   trunk/moon/src/asf/asf-structures.h
   trunk/moon/src/asf/asf.cpp
   trunk/moon/src/asf/asf.cs
   trunk/moon/src/asf/asf.h
   trunk/moon/src/media.cpp
Log:
* asf/asf.cpp, asf/asf.cs, asf/asf.h, asf/asf-generated.cpp,
  asf/asf-generated.h, asf/asf-structures.cpp, asf/asf-structures.h,
  media.cpp: Redo ASFParser::Malloc to avoid warnings, and make sure
  the return value is always checked.

Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2007-10-31 19:16:45 UTC (rev 88603)
+++ trunk/moon/src/ChangeLog    2007-10-31 19:17:34 UTC (rev 88604)
@@ -1,3 +1,8 @@
+2007-10-31  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
+
+       * media.cpp: Redo ASFParser::Malloc to avoid warnings, and make sure the
+         return value is always checked.
+
 2007-10-31  Chris Toshok  <[EMAIL PROTECTED]>
 
        * trigger.cpp (EventTrigger::SetTarget,

Modified: trunk/moon/src/asf/ChangeLog
===================================================================
--- trunk/moon/src/asf/ChangeLog        2007-10-31 19:16:45 UTC (rev 88603)
+++ trunk/moon/src/asf/ChangeLog        2007-10-31 19:17:34 UTC (rev 88604)
@@ -1,3 +1,9 @@
+2007-10-31  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
+
+       * asf.cpp, asf.cs, asf.h, asf-generated.cpp, asf-generated.h,
+         asf-structures.cpp, asf-structures.h: Redo ASFParser::Malloc to avoid
+         warnings, and make sure the return value is always checked.
+
 2007-10-30  Jeffrey Stedfast  <[EMAIL PROTECTED]>
 
        * asf-structures.cpp (FillInAll): max_packet_size is part of

Modified: trunk/moon/src/asf/asf-generated.cpp
===================================================================
--- trunk/moon/src/asf/asf-generated.cpp        2007-10-31 19:16:45 UTC (rev 
88603)
+++ trunk/moon/src/asf/asf-generated.cpp        2007-10-31 19:17:34 UTC (rev 
88604)
@@ -238,8 +238,6 @@
                asf_header_extension_dump ((asf_header_extension*) obj); break;
        case ASF_CODEC_LIST:
                asf_codec_list_dump ((asf_codec_list*) obj); break;
-       case ASF_SCRIPT_COMMAND:
-               asf_script_command_dump ((asf_script_command*) obj); break;
        case ASF_MARKER:
                asf_marker_dump ((asf_marker*) obj); break;
        case ASF_BITRATE_MUTUAL_EXCLUSION:

Modified: trunk/moon/src/asf/asf-generated.h
===================================================================
--- trunk/moon/src/asf/asf-generated.h  2007-10-31 19:16:45 UTC (rev 88603)
+++ trunk/moon/src/asf/asf-generated.h  2007-10-31 19:17:34 UTC (rev 88604)
@@ -75,7 +75,6 @@
 void asf_stream_properties_dump (const asf_stream_properties* obj);
 void asf_header_extension_dump (const asf_header_extension* obj);
 void asf_codec_list_dump (const asf_codec_list* obj);
-void asf_script_command_dump (const asf_script_command* obj);
 void asf_marker_dump (const asf_marker* obj);
 void asf_bitrate_mutual_exclusion_dump (const asf_bitrate_mutual_exclusion* 
obj);
 void asf_error_correction_dump (const asf_error_correction* obj);

Modified: trunk/moon/src/asf/asf-structures.cpp
===================================================================
--- trunk/moon/src/asf/asf-structures.cpp       2007-10-31 19:16:45 UTC (rev 
88603)
+++ trunk/moon/src/asf/asf-structures.cpp       2007-10-31 19:17:34 UTC (rev 
88604)
@@ -390,7 +390,12 @@
                        parser->AddError ("Data corruption in payload.");
                        return false;
                }
-               replicated_data = (asf_byte*) g_malloc0 
(replicated_data_length);
+               
+               replicated_data = (asf_byte*) parser->MallocVerified 
(replicated_data_length);
+               if (replicated_data == NULL) {
+                       return false;
+               }
+               
                if (!source->Read (replicated_data, replicated_data_length))
                        return false;
        }
@@ -435,7 +440,11 @@
                        return false;
                }
                
-               payload_data = (asf_byte*) g_malloc0 (payload_data_length);
+               payload_data = (asf_byte*) parser->MallocVerified 
(payload_data_length);
+               if (payload_data == NULL) {
+                       return false;
+               }
+               
                if (!source->Read (payload_data, payload_data_length))
                        return false;
        }
@@ -483,7 +492,10 @@
                return false;
        }
 
-       payloads = (asf_single_payload**) g_malloc0 (sizeof 
(asf_single_payload*) * (count + 1));
+       payloads = (asf_single_payload**) parser->MallocVerified (sizeof 
(asf_single_payload*) * (count + 1));
+       if (payloads == NULL) {
+               return false;
+       }
        
        ASF_LOG ("asf_multiple_payloads::FillInAll (): Reading %i 
payloads...\n", count); 
        
@@ -516,6 +528,99 @@
        }
 }
 
+asf_script_command_entry** 
+asf_script_command::get_commands (ASFParser* parser, char*** command_types)
+{
+       gint32 size_left = size;
+       gint32 size_requested = 0;
+       char** types = NULL;
+       char* start = NULL;
+       asf_script_command_entry** result = NULL;
+       asf_script_command_entry* next = NULL;
+       
+       if (size == sizeof (asf_script_command))
+               return NULL;
+       
+       size_left -= sizeof (asf_script_command);
+       
+       size_requested = sizeof (char*) * (command_count + 1);
+       if (size_requested > size_left) {
+               parser->AddError ("Data corruption in script command.");
+               goto failure;
+       }
+       size_left -= size_requested;
+       result = (asf_script_command_entry**) parser->MallocVerified 
(size_requested);
+       if (result == NULL)
+               goto failure;
+       
+       size_requested = sizeof (char*) * (command_type_count + 1);
+       if (size_requested > size_left) {
+               parser->AddError ("Data corruption in script command.");
+               goto failure;
+       }
+       size_left -= size_requested;
+       types = (char**) parser->MallocVerified (size_requested);
+       if (types == NULL)
+               goto failure;
+       
+       if (command_types != NULL) 
+               *command_types = types;
+
+       // Walk past by the command type table.
+       start = (sizeof (asf_script_command) + (char*) this);
+       for (gint32 i = 0; i < command_type_count; i++) {
+               asf_word length = * (asf_word*) start;
+               
+               // Verify data
+               size_requested = sizeof (asf_word) + sizeof (guint16) * length;
+               if (size_requested > size_left) {
+                       parser->AddError ("Data corruption in script command.");
+                       goto failure;
+               }
+               size_left -= size_requested;
+               
+               // Convert strings
+               types [i] = wchar_to_utf8 (start + sizeof (asf_word), length);
+               start += size_requested;
+       }
+       
+       // Fill in the commands table
+       next = (asf_script_command_entry*) start;
+       for (gint32 i = 0; i < command_count; i++) {
+               result [i] = next;
+               
+               char* tmp = (char*) next;
+               tmp += sizeof (asf_script_command_entry);
+               tmp += (next->name_length * sizeof (guint16));
+               
+               size_requested = sizeof (asf_script_command_entry) + 
next->name_length * sizeof (guint16);
+               if (size_requested > size_left) {
+                       parser->AddError ("Data corruption in script command.");
+                       goto failure;
+               }
+               size_left -= size_requested;
+               
+               next = (asf_script_command_entry*) tmp;
+       }
+       
+       return result;
+       
+failure:
+       g_free (result);
+       if (types != NULL) {
+               int i = -1;
+               while (types [++i] != NULL)
+                       g_free (types [i]);
+               g_free (types);
+       }
+       
+       if (command_types != NULL)
+               *command_types = NULL;
+               
+       return NULL;
+}
+
+
 void asf_marker_entry_dump (const asf_marker_entry* obj)
 {
        asf_marker_entry* o = (asf_marker_entry*) obj;
@@ -548,7 +653,7 @@
        }
 }
 
-void asf_script_command_dump (const asf_script_command* obj)
+void asf_script_command_dump (ASFParser* parser, const asf_script_command* obj)
 {
        asf_script_command* o = (asf_script_command*) obj;
        
@@ -562,7 +667,7 @@
        char** command_types = NULL;
        asf_script_command_entry** entries = NULL;
        
-       entries = o->get_commands (&command_types);
+       entries = o->get_commands (parser, &command_types);
        
        for (guint32 i = 0; i < obj->command_type_count; i++) {
                ASF_DUMP ("\tASF_SCRIPT_COMMAND_TYPE #%i\n", i);

Modified: trunk/moon/src/asf/asf-structures.h
===================================================================
--- trunk/moon/src/asf/asf-structures.h 2007-10-31 19:16:45 UTC (rev 88603)
+++ trunk/moon/src/asf/asf-structures.h 2007-10-31 19:17:34 UTC (rev 88604)
@@ -335,6 +335,7 @@
 };
 
 void asf_multiple_payloads_dump (asf_multiple_payloads* obj);
+void asf_script_command_dump (ASFParser* parser, const asf_script_command* 
obj);
 
 struct asf_object {
        asf_guid id;
@@ -462,85 +463,7 @@
        // A NULL terminated array of commands.
        // command_types: an array of strings, use g_free on the elements and 
the array.
        // result: Free the array with g_free (do NOT free each element).
-       asf_script_command_entry** get_commands (char*** command_types)
-       {
-               asf_script_command_entry **result = NULL;
-               asf_script_command_entry *next = NULL;
-               gint32 size_left = size;
-               gint32 size_requested = 0;
-               char **types = NULL;
-               char *start = NULL;
-               char *tmp;
-               
-               if (size == sizeof (asf_script_command))
-                       return NULL;
-               
-               size_left -= sizeof (asf_script_command);
-               
-               size_requested = sizeof (char*) * (command_count + 1);
-               if (size_requested > size_left) 
-                       goto failure;
-               size_left -= size_requested;
-               result = (asf_script_command_entry **) g_malloc0 
(size_requested);
-               
-               size_requested = sizeof (char*) * (command_type_count + 1);
-               if (size_requested > size_left)
-                       goto failure;
-               size_left -= size_requested;
-               types = (char **) g_malloc0 (size_requested);
-               
-               if (command_types != NULL) 
-                       *command_types = types;
-
-               // Walk past by the command type table.
-               start = (sizeof (asf_script_command) + (char *) this);
-               for (gint32 i = 0; i < command_type_count; i++) {
-                       asf_word length = * (asf_word*) start;
-                       
-                       // Verify data
-                       size_requested = sizeof (asf_word) + sizeof (guint16) * 
length;
-                       if (size_requested > size_left)
-                               goto failure;
-                       size_left -= size_requested;
-                       
-                       // Convert strings
-                       types [i] = wchar_to_utf8 (start + sizeof (asf_word), 
length);
-                       start += size_requested;
-               }
-               
-               // Fill in the commands table
-               next = (asf_script_command_entry*) start;
-               for (gint32 i = 0; i < command_count; i++) {
-                       result [i] = next;
-                       
-                       tmp = (char *) next;
-                       tmp += sizeof (asf_script_command_entry);
-                       tmp += (next->name_length * sizeof (guint16));
-                       
-                       size_requested = sizeof (asf_script_command_entry) + 
next->name_length * sizeof (guint16);
-                       if (size_requested > size_left)
-                               goto failure;
-                       size_left -= size_requested;
-                       
-                       next = (asf_script_command_entry*) tmp;
-               }
-               
-               return result;
-               
-failure:
-               g_free (result);
-               if (types != NULL) {
-                       int i = -1;
-                       while (types [++i] != NULL)
-                               g_free (types [i]);
-                       g_free (types);
-               }
-               
-               if (command_types != NULL)
-                       *command_types = NULL;
-                       
-               return NULL;
-       }
+       asf_script_command_entry** get_commands (ASFParser* parser, char*** 
command_types);
 };
 
 struct asf_marker_entry {

Modified: trunk/moon/src/asf/asf.cpp
===================================================================
--- trunk/moon/src/asf/asf.cpp  2007-10-31 19:16:45 UTC (rev 88603)
+++ trunk/moon/src/asf/asf.cpp  2007-10-31 19:17:34 UTC (rev 88604)
@@ -65,7 +65,7 @@
 }
 
 bool
-ASFParser::VerifyHeaderDataSize (gint32 size)
+ASFParser::VerifyHeaderDataSize (guint64 size)
 {
        if (header == NULL)
                return false;
@@ -73,25 +73,25 @@
        return size >= 0 && size < header->size;
 }
 
-bool
-ASFParser::Malloc (void** mem, gint32 size)
+void* 
+ASFParser::Malloc (gint32 size)
 {
-       if (mem == NULL) {
-               AddError ("Memory corruption."); // This shouldn't happen ever.
-               return false;
-       }
-       
        if (!VerifyHeaderDataSize (size))
-               return false;
+               return NULL;
                
-       *mem = g_malloc0 (size);
+       return MallocVerified (size);
+}
+
+void*
+ASFParser::MallocVerified (gint32 size)
+{
+       void* result = g_try_malloc0 (size);
        
-       if (*mem == NULL) {
+       if (result == NULL) {
                AddError ("Out of memory.");
-               return false;
        }
        
-       return true;
+       return result;
 }
 
 asf_object*
@@ -109,17 +109,13 @@
                return NULL;
        }
        
-       if (!Malloc ((void**)&result, obj->size)) {
+       result = (asf_object*) Malloc (obj->size);
+       if (result == NULL) {
                char* g = asf_guid_tostring (&obj->id);
                AddError (g_strdup_printf ("Header corrupted (id: %s)", g));
                g_free (g);
                return false;
        }
-       
-       if (result == NULL) {
-               AddError ("Out of memory.\n");
-               return NULL;
-       }
 
        memcpy (result, obj, sizeof (asf_object));
 
@@ -187,7 +183,8 @@
                        return false;
                }
                //asf_single_payload_dump (sp);
-               if (!Malloc ((void**) &mp->payloads, sizeof 
(asf_single_payload*) * 2)) {
+               mp->payloads = (asf_single_payload**) Malloc (sizeof 
(asf_single_payload*) * 2);
+               if (mp->payloads == NULL) {
                        AddError ("Data corruption in payloads.");
                        return false;
                }
@@ -224,7 +221,8 @@
        
        ASF_LOG ("Current position: %llx (%lld)\n", source->Position (), 
source->Position ());
        
-       if (!Malloc ((void**) &data, sizeof (asf_data))) {
+       data = (asf_data*) Malloc (sizeof (asf_data));
+       if (data == NULL) {
                AddError ("Data corruption in data.");
                return false;
        }
@@ -249,10 +247,8 @@
 {
        ASF_LOG ("ASFParser::ReadHeader ().\n");
        
-       header = (asf_header*) g_malloc0 (sizeof (asf_header));
-       
+       header = (asf_header*) MallocVerified (sizeof (asf_header));
        if (header == NULL) {
-               AddError ("Out of memory.");
                return false;
        }
        
@@ -266,7 +262,8 @@
                return false;
        }
        
-       if (!Malloc ((void**) &header_objects, (header->object_count + 1) * 
sizeof (asf_object*))) {
+       header_objects = (asf_object**) Malloc ((header->object_count + 1) * 
sizeof (asf_object*));
+       if (header_objects == NULL) {
                AddError ("Data corruption in header.");
                return false;
        }
@@ -569,7 +566,8 @@
        // so we allocate space for two (+ NULL at the end).
        if (payloads == NULL) {
                payloads_size = 2;
-               if (!parser->Malloc ((void**) &payloads, sizeof 
(asf_single_payload*) * (payloads_size + 1))) {
+               payloads = (asf_single_payload**) parser->Malloc (sizeof 
(asf_single_payload*) * (payloads_size + 1));
+               if (payloads == NULL) {
                        parser->AddError ("Data corruption in packets.");
                        return false;
                }
@@ -637,7 +635,13 @@
                                size += current->payload->payload_data_length;
                                if (payload_count > payloads_size) {
                                        gint32 new_size = payload_count + 3;
-                                       payloads = (asf_single_payload**) 
g_realloc (payloads, sizeof (asf_single_payload*) * (new_size + 1));
+                                       asf_single_payload** new_payloads = 
NULL;
+                                       new_payloads = (asf_single_payload**) 
g_try_realloc (payloads, sizeof (asf_single_payload*) * (new_size + 1));
+                                       if (new_payloads == NULL) {
+                                               parser->AddError ("Out of 
memory.");
+                                               return false;
+                                       }
+                                       payloads = new_payloads;
                                        memset (payloads + payloads_size, 0, 
sizeof (asf_single_payload*) * (new_size - payloads_size + 1));
                                        payloads_size = new_size;
                                }

Modified: trunk/moon/src/asf/asf.cs
===================================================================
--- trunk/moon/src/asf/asf.cs   2007-10-31 19:16:45 UTC (rev 88603)
+++ trunk/moon/src/asf/asf.cs   2007-10-31 19:17:34 UTC (rev 88604)
@@ -118,7 +118,9 @@
                        writer.WriteLine ("/* Debug functions */ ");
                        
                        foreach (string str in debug_structs) {
-                               writer.WriteLine ("void " + str + "_dump (const 
" + str + "* obj);");
+                               if (!File.ReadAllText 
("asf-structures.h").Contains ("void " + str + "_dump (")) {
+                                       writer.WriteLine ("void " + str + 
"_dump (const " + str + "* obj);");
+                               }
                        }
                        writer.WriteLine ("");
                        
@@ -187,6 +189,8 @@
                        foreach (string str in objects) {
                                if (str == "asf_object")
                                        continue;
+                               if (File.ReadAllText 
("asf-structures.h").Contains ("void " + str + "_dump ("))
+                                       continue;
                                writer.WriteLine ("\tcase {0}:", str.ToUpper 
());
                                writer.WriteLine ("\t\t{0}_dump (({0}*) obj); 
break;", str);
                        }

Modified: trunk/moon/src/asf/asf.h
===================================================================
--- trunk/moon/src/asf/asf.h    2007-10-31 19:16:45 UTC (rev 88603)
+++ trunk/moon/src/asf/asf.h    2007-10-31 19:17:34 UTC (rev 88604)
@@ -30,7 +30,6 @@
 class ASFFileSource;
 class ASFSource;
 
-
 #define ASF_ERROR_VAL(fail, ...) { fprintf (stderr, __VA_ARGS__); return fail; 
}
 #define ASF_ERROR(...) ASF_ERROR_VAL(false, __VA_ARGS__)
 
@@ -281,8 +280,9 @@
        
        asf_object* ReadObject (asf_object* guid);
        const char* GetLastError ();
-       bool VerifyHeaderDataSize (gint32 size); // Verifies that the requested 
size is a size that can be inside the header.
-       bool Malloc (void** mem, gint32 size); // Allocates the requested 
memory and verifies that the size can actually be contained within the header.
+       bool VerifyHeaderDataSize (guint64 size); // Verifies that the 
requested size is a size that can be inside the header.
+       void* Malloc (gint32 size); // Allocates the requested memory and 
verifies that the size can actually be contained within the header. Reports an 
Out of Memory error if the memory can't be allocated, and returns NULL
+       void* MallocVerified (gint32 size); // Allocates the requested memory 
(no size checking), reports an Out of Memory error if the memory can't be 
allocated, and returns NULL
        void AddError (const char* err); // Makes a copy of the provided error 
string.
        void AddError (char* err); // Frees the provided error string.
 

Modified: trunk/moon/src/media.cpp
===================================================================
--- trunk/moon/src/media.cpp    2007-10-31 19:16:45 UTC (rev 88603)
+++ trunk/moon/src/media.cpp    2007-10-31 19:17:34 UTC (rev 88604)
@@ -245,7 +245,7 @@
        TimelineMarkerCollection* col = NULL;
        int i = -1;
        
-       commands = command->get_commands (&command_types);
+       commands = command->get_commands (mplayer->asf_parser, &command_types);
        
        if (commands == NULL)
                goto cleanup;

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to