widl: Replace strdup, malloc and realloc with xstrdup, xmalloc and xrealloc

Dan Hipschman dsh at linux.ucla.edu
Fri Aug 4 13:06:54 CDT 2006


A lot of uses of strdup and realloc were going unchecked, and one instance
of malloc was checked but no error was reported if it failed.  There's not
much of a sensible recovery strategy for WIDL if it can't allocate memory,
anyway.  This patch let's WIDL die more gracefully.

ChangeLog:
* Replace strdup, malloc and realloc with x- variants (e.g., xstrdup)
---
 tools/widl/widl.c       |   12 ++++++------
 tools/widl/write_msft.c |   19 ++-----------------
 2 files changed, 8 insertions(+), 23 deletions(-)

diff --git a/tools/widl/widl.c b/tools/widl/widl.c
index 152d35f..aac1d5e 100644
--- a/tools/widl/widl.c
+++ b/tools/widl/widl.c
@@ -189,7 +189,7 @@ #endif
       do_client = 1;
       break;
     case 'C':
-      client_name = strdup(optarg);
+      client_name = xstrdup(optarg);
       break;
     case 'd':
       debuglevel = strtol(optarg, NULL, 0);
@@ -206,7 +206,7 @@ #endif
       do_header = 1;
       break;
     case 'H':
-      header_name = strdup(optarg);
+      header_name = xstrdup(optarg);
       break;
     case 'I':
       wpp_add_include_path(optarg);
@@ -219,28 +219,28 @@ #endif
       do_proxies = 1;
       break;
     case 'P':
-      proxy_name = strdup(optarg);
+      proxy_name = xstrdup(optarg);
       break;
     case 's':
       do_everything = 0;
       do_server = 1;
       break;
     case 'S':
-      server_name = strdup(optarg);
+      server_name = xstrdup(optarg);
       break;
     case 't':
       do_everything = 0;
       do_typelib = 1;
       break;
     case 'T':
-      typelib_name = strdup(optarg);
+      typelib_name = xstrdup(optarg);
       break;
     case 'u':
       do_everything = 0;
       do_idfile = 1;
       break;
     case 'U':
-      idfile_name = strdup(optarg);
+      idfile_name = xstrdup(optarg);
       break;
     case 'V':
       printf(version_string);
diff --git a/tools/widl/write_msft.c b/tools/widl/write_msft.c
index 8148a93..cac1f7f 100644
--- a/tools/widl/write_msft.c
+++ b/tools/widl/write_msft.c
@@ -362,7 +362,6 @@ static int ctl2_encode_string(
  * RETURNS
  *
  *  Success: The offset within the segment of the new data area.
- *  Failure: -1 (this is invariably an out of memory condition).
  *
  * BUGS
  *
@@ -389,8 +388,7 @@ static int ctl2_alloc_segment(
 	char *block;
 
 	block_size = typelib->typelib_segment_block_length[segment];
-	block = realloc(typelib->typelib_segment_data[segment], block_size << 1);
-	if (!block) return -1;
+	block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
 
 	if (segment == MSFT_SEG_TYPEINFO) {
 	    /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
@@ -430,7 +428,6 @@ static int ctl2_alloc_typeinfo(
     MSFT_TypeInfoBase *typeinfo;
 
     offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
-    if (offset == -1) return -1;
 
     typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
 
@@ -475,7 +472,6 @@ static int ctl2_alloc_typeinfo(
  * RETURNS
  *
  *  Success: The offset of the new GUID.
- *  Failure: -1 (this is invariably an out of memory condition).
  */
 static int ctl2_alloc_guid(
 	msft_typelib_t *typelib,   /* [I] The type library to allocate in. */
@@ -491,7 +487,6 @@ static int ctl2_alloc_guid(
     if (offset != -1) return offset;
 
     offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
-    if (offset == -1) return -1;
 
     guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
     *guid_space = *guid;
@@ -528,7 +523,6 @@ static int ctl2_alloc_name(
     if (offset != -1) return offset;
 
     offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
-    if (offset == -1) return -1;
 
     name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
     name_space->hreftype = -1;
@@ -574,7 +568,6 @@ static int ctl2_alloc_string(
     }
 
     offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
-    if (offset == -1) return -1;
 
     string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
     memcpy(string_space, encoded_string, length);
@@ -611,7 +604,6 @@ static int alloc_msft_importinfo(
     impinfo->flags |= typelib->typelib_header.nimpinfos++;
 
     offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
-    if (offset == -1) return -1;
 
     impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
     *impinfo_space = *impinfo;
@@ -653,7 +645,6 @@ static int alloc_importfile(
     }
 
     offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
-    if (offset == -1) return -1;
 
     importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
     importfile->guid = guidoffset;
@@ -1233,11 +1224,9 @@ static HRESULT set_custdata(msft_typelib
     guidentry.next_hash = -1;
 
     guidoffset = ctl2_alloc_guid(typelib, &guidentry);
-    if (guidoffset == -1) return E_OUTOFMEMORY;
     write_value(typelib, &data_out, vt, value);
 
     custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
-    if (custoffset == -1) return E_OUTOFMEMORY;
 
     custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
     custdata[0] = guidoffset;
@@ -2262,9 +2251,6 @@ static void set_guid(msft_typelib_t *typ
     }
 
     offset = ctl2_alloc_guid(typelib, &guidentry);
-    
-    if (offset == -1) return;
-
     typelib->typelib_header.posguid = offset;
 
     return;
@@ -2527,8 +2513,7 @@ int create_msft_typelib(typelib_t *typel
     GUID midl_time_guid    = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}}; 
     GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}}; 
 
-    msft = malloc(sizeof(*msft));
-    if (!msft) return 0;
+    msft = xmalloc(sizeof(*msft));
     memset(msft, 0, sizeof(*msft));
     msft->typelib = typelib;
 



More information about the wine-patches mailing list