cluster: STABLE3 - libfence: replace strcat

David Teigland teigland at fedoraproject.org
Mon Oct 12 19:21:58 UTC 2009


Gitweb:        http://git.fedorahosted.org/git/cluster.git?p=cluster.git;a=commitdiff;h=d02babbaf18c9fc3449ac744444b50fc45e7406b
Commit:        d02babbaf18c9fc3449ac744444b50fc45e7406b
Parent:        b2230a5adff95b1db62deb9acfbf0f37553101b9
Author:        David Teigland <teigland at redhat.com>
AuthorDate:    Mon Oct 12 14:19:23 2009 -0500
Committer:     David Teigland <teigland at redhat.com>
CommitterDate: Mon Oct 12 14:19:23 2009 -0500

libfence: replace strcat

with snprintf which is just better and safer all around.

Signed-off-by: David Teigland <teigland at redhat.com>
---
 fence/libfence/agent.c |   76 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/fence/libfence/agent.c b/fence/libfence/agent.c
index 4c19f10..5577fda 100644
--- a/fence/libfence/agent.c
+++ b/fence/libfence/agent.c
@@ -113,15 +113,19 @@ static int run_agent(char *agent, char *args, int *agent_result)
 static int make_args(int cd, char *victim, char *method, int d,
 		     char *device, char **args_out)
 {
-	char path[PATH_MAX], arg[PATH_MAX];
+	char path[PATH_MAX];
 	char *args, *str;
-	int error, cnt = 0;
+	int error, ret, cnt = 0;
+	size_t len, pos;
 
 	args = malloc(FENCE_AGENT_ARGS_MAX);
 	if (!args)
 		return -ENOMEM;
 	memset(args, 0, FENCE_AGENT_ARGS_MAX);
 
+	len = FENCE_AGENT_ARGS_MAX - 1;
+	pos = 0;
+
 	/* node-specific args for victim */
 
 	memset(path, 0, PATH_MAX);
@@ -138,17 +142,26 @@ static int make_args(int cd, char *victim, char *method, int d,
 			continue;
 		}
 
-		strcat(args, str);
-		strcat(args, "\n");
+		ret = snprintf(args + pos, len - pos, "%s\n", str);
+
 		free(str);
+
+		if (ret >= len - pos) {
+			error = -E2BIG;
+			goto out;
+		}
+		pos += ret;
 	}
 
 	/* add nodename of victim to args */
 
 	if (!strstr(args, "nodename=")) {
-		memset(arg, 0, PATH_MAX);
-		snprintf(arg, PATH_MAX, "nodename=%s\n", victim);
-		strcat(args, arg);
+		ret = snprintf(args + pos, len - pos, "nodename=%s\n", victim);
+		if (ret >= len - pos) {
+			error = -E2BIG;
+			goto out;
+		}
+		pos += ret;
 	}
 
 	/* device-specific args */
@@ -167,13 +180,20 @@ static int make_args(int cd, char *victim, char *method, int d,
 			continue;
 		}
 
-		strcat(args, str);
-		strcat(args, "\n");
+		ret = snprintf(args + pos, len - pos, "%s\n", str);
+
 		free(str);
+
+		if (ret >= len - pos) {
+			error = -E2BIG;
+			goto out;
+		}
+		pos += ret;
 	}
 
 	if (cnt)
 		error = 0;
+ out:
 	if (error) {
 		free(args);
 		args = NULL;
@@ -412,15 +432,19 @@ int fence_node(char *victim, struct fence_log *log, int log_size,
 static int make_args_unfence(int cd, char *victim, int d,
 			     char *device, char **args_out)
 {
-	char path[PATH_MAX], arg[PATH_MAX];
+	char path[PATH_MAX];
 	char *args, *str;
-	int error, cnt = 0;
+	int error, ret, cnt = 0;
+	size_t len, pos;
 
 	args = malloc(FENCE_AGENT_ARGS_MAX);
 	if (!args)
 		return -ENOMEM;
 	memset(args, 0, FENCE_AGENT_ARGS_MAX);
 
+	len = FENCE_AGENT_ARGS_MAX - 1;
+	pos = 0;
+
 	/* node-specific args for victim */
 
 	memset(path, 0, PATH_MAX);
@@ -437,17 +461,26 @@ static int make_args_unfence(int cd, char *victim, int d,
 			continue;
 		}
 
-		strcat(args, str);
-		strcat(args, "\n");
+		ret = snprintf(args + pos, len - pos, "%s\n", str);
+
 		free(str);
+		
+		if (ret >= len - pos) {
+			error = -E2BIG;
+			goto out;
+		}
+		pos += ret;
 	}
 
 	/* add nodename of victim to args */
 
 	if (!strstr(args, "nodename=")) {
-		memset(arg, 0, PATH_MAX);
-		snprintf(arg, PATH_MAX, "nodename=%s\n", victim);
-		strcat(args, arg);
+		ret = snprintf(args + pos, len - pos, "nodename=%s\n", victim);
+		if (ret >= len - pos) {
+			error = -E2BIG;
+			goto out;
+		}
+		pos += ret;
 	}
 
 	/* device-specific args */
@@ -466,13 +499,20 @@ static int make_args_unfence(int cd, char *victim, int d,
 			continue;
 		}
 
-		strcat(args, str);
-		strcat(args, "\n");
+		ret = snprintf(args + pos, len - pos, "%s\n", str);
+
 		free(str);
+
+		if (ret >= len - pos) {
+			error = -E2BIG;
+			goto out;
+		}
+		pos += ret;
 	}
 
 	if (cnt)
 		error = 0;
+ out:
 	if (error) {
 		free(args);
 		args = NULL;


More information about the cluster-commits mailing list