Просмотр исходного кода

rewrite strstr_rs not to use strdup

strstr_rs used strdup and didn't handle failure.  This change removes
the use of strdup as well as the uses of strstr, since all callers
passed a string of length 1 as the second argument.  This also changes
the prototype so that the 2nd parameter is a byte, not a string.

* util.h (strstr_rs): Adjust prototype.
* util.c (strstr_rs): Rewrite/simplify.
* sa-confdb.c (strstr_rs): Remove duplicate definition.
* coroparse.c (parse_section): Update callers.

git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@2111 fd59a12c-fef9-0310-b244-a6a79926bd2f
Jim Meyering 17 лет назад
Родитель
Сommit
89c6d6a1a1
4 измененных файлов с 8 добавлено и 55 удалено
  1. 3 3
      exec/coroparse.c
  2. 4 21
      exec/util.c
  3. 1 1
      exec/util.h
  4. 0 30
      lib/sa-confdb.c

+ 3 - 3
exec/coroparse.c

@@ -115,7 +115,7 @@ static int parse_section(FILE *fp,
 		}
 
 		/* New section ? */
-		if ((loc = strstr_rs (line, "{"))) {
+		if ((loc = strstr_rs (line, '{'))) {
 			hdb_handle_t new_parent;
 			char *section = remove_whitespace(line);
 
@@ -128,7 +128,7 @@ static int parse_section(FILE *fp,
 		}
 
 		/* New key/value */
-		if ((loc = strstr_rs (line, ":"))) {
+		if ((loc = strstr_rs (line, ':'))) {
 			char *key;
 			char *value;
 
@@ -140,7 +140,7 @@ static int parse_section(FILE *fp,
 				value, strlen (value) + 1);
 		}
 
-		if ((loc = strstr_rs (line, "}"))) {
+		if ((loc = strstr_rs (line, '}'))) {
 			return 0;
 		}
 	}

+ 4 - 21
exec/util.c

@@ -110,32 +110,15 @@ char *getcs_name_t (cs_name_t *name)
 	return ((char *)name->value);
 }
 
-char *strstr_rs (const char *haystack, const char *needle)
+char *strstr_rs (const char *haystack, int byte)
 {
-	char *end_address;
-	char *new_needle;
-
-	new_needle = (char *)strdup (needle);
-	new_needle[strlen (new_needle) - 1] = '\0';
-
-	end_address = strstr (haystack, new_needle);
-	if (end_address) {
-		end_address += strlen (new_needle);
-		end_address = strstr (end_address, needle + strlen (new_needle));
-	}
+	const char *end_address = strchr (haystack, byte);
 	if (end_address) {
 		end_address += 1; /* skip past { or = */
-		do {
-			if (*end_address == '\t' || *end_address == ' ') {
-				end_address++;
-			} else {
-				break;
-			}
-		} while (*end_address != '\0');
+		end_address += strspn (end_address, " \t");
 	}
 
-	free (new_needle);
-	return (end_address);
+	return ((char *) end_address);
 }
 
 void setcs_name_t (cs_name_t *name, char *str) {

+ 1 - 1
exec/util.h

@@ -73,7 +73,7 @@ extern void _corosync_exit_error (enum e_ais_done err, const char *file,
   __attribute__((__noreturn__));
 void _corosync_out_of_memory_error (void) __attribute__((__noreturn__));
 extern char *getcs_name_t (cs_name_t *name);
-extern char *strstr_rs (const char *haystack, const char *needle);
+extern char *strstr_rs (const char *haystack, int byte);
 extern void setcs_name_t (cs_name_t *name, char *str);
 extern int cs_name_tisEqual (cs_name_t *str1, char *str2);
 #endif /* UTIL_H_DEFINED */

+ 0 - 30
lib/sa-confdb.c

@@ -62,7 +62,6 @@ static int num_config_modules;
 static struct config_iface_ver0 *config_modules[128];
 
 void main_get_config_modules(struct config_iface_ver0 ***modules, int *num);
-char *strstr_rs (const char *haystack, const char *needle);
 
 static int load_objdb(void)
 {
@@ -149,35 +148,6 @@ void main_get_config_modules(struct config_iface_ver0 ***modules, int *num)
 	*num = num_config_modules;
 }
 
-/* Needed by some modules ... */
-char *strstr_rs (const char *haystack, const char *needle)
-{
-	char *end_address;
-	char *new_needle;
-
-	new_needle = (char *)strdup (needle);
-	new_needle[strlen (new_needle) - 1] = '\0';
-
-	end_address = strstr (haystack, new_needle);
-	if (end_address) {
-		end_address += strlen (new_needle);
-		end_address = strstr (end_address, needle + strlen (new_needle));
-	}
-	if (end_address) {
-		end_address += 1; /* skip past { or = */
-		do {
-			if (*end_address == '\t' || *end_address == ' ') {
-				end_address++;
-			} else {
-				break;
-			}
-		} while (*end_address != '\0');
-	}
-
-	free (new_needle);
-	return (end_address);
-}
-
 int confdb_sa_init (void)
 {
 	int res;