[ofw] [PATCH][REPOST] opensm/libvendor/winosm_common.c: Better failure handling in strdup_expand and Fopen

Hal Rosenstock hal at dev.mellanox.co.il
Wed Apr 11 07:34:00 PDT 2012


Return NULL when (underlying) malloc fails 

Signed-off-by: Hal Rosenstock <hal at mellanox.com>
---
Index: libvendor/winosm_common.c
===================================================================
--- libvendor/winosm_common.c	(revision 3395)
+++ libvendor/winosm_common.c	(working copy)
@@ -52,9 +52,12 @@
 	char *fname;
 
 	fname = strdup_expand(filename);
-	pFile = fopen(fname,mode);
-	free(fname);
-	return pFile;
+	if (fname) {
+		pFile = fopen(fname,mode);
+		free(fname);
+		return pFile;
+	}
+	return NULL;
 }
 
 #define OSM_MAX_LOG_NAME_SIZE 512
@@ -177,7 +180,7 @@
 
 
 /*
- * Like _strdup() with Environment varible expansion.
+ * Like _strdup() with Environment variable expansion.
  * Example: str '%windir%\temp\osm.log' --> 'C:\windows\temp\osm.log'
  * Multiple Env vars are supported.
  */
@@ -188,6 +191,8 @@
 	char p_env[80];
 
 	str = _strdup(base);
+	if (!str)
+		return str;
 
 	while( (s = strchr(str,'%')) )
 	{
@@ -203,10 +208,12 @@
 			return str;
 
 		xs = (char*)malloc(strlen(str)+strlen(es));
-		for(rc=str,n=xs; rc < (s-1);rc++) *n++ = *rc; 
-		*n='\0';
-		strcat(n,es);
-		strcat(n,(p+1));
+		if (xs) {
+			for(rc=str,n=xs; rc < (s-1);rc++) *n++ = *rc; 
+			*n='\0';
+			strcat(n,es);
+			strcat(n,(p+1));
+		}
 		free(str);
 		str = xs;
 	}



More information about the ofw mailing list