Skip to contentSkip to conversation

perf tools: Add error handling

PATCH Superseded Firo Yang 2 participants 2 messages Jun 9, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Firo Yang

author
#0 lore raw

PATCH 1/1 perf tools: Add error handling

Add error handling code for snprintf and rename in check_backup.

---
 tools/perf/util/data.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/data.c +14 -3
index 1921942..d3a7363 100644
@@ -32,15 +32,26 @@ static bool check_pipe(struct perf_data_file *file)
 
 static int check_backup(struct perf_data_file *file)
 {
+	int ret;
 	struct stat st;
 
 	if (!stat(file->path, &st) && st.st_size) {
-		/* TODO check errors properly */
 		char oldname[PATH_MAX];
-		snprintf(oldname, sizeof(oldname), "%s.old",
+		ret = snprintf(oldname, sizeof(oldname), "%s.old",
 			 file->path);
+		if (ret < 0) {
+			pr_err("failed to make name %s.old\n", file->path);
+			return -1;
+		}
+
 		unlink(oldname);
-		rename(file->path, oldname);
+
+		ret = rename(file->path, oldname);
+		if (ret < 0) {
+			pr_err("failed to rename %s to %s\n", file->path,
+				oldname);
+			return -1;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.4.2
#1 lore raw
Quoted 17 lines · Firo Yang

Add error handling code for snprintf and rename in check_backup.

---
 tools/perf/util/data.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/data.c +1 -0
index 1921942..d3a7363 100644
@@ -32,15 +32,26 @@ static bool check_pipe(struct perf_data_file *file)
 
 static int check_backup(struct perf_data_file *file)
 {
+	int ret;
 	struct stat st;

tools/perf/util/data.c static bool check_pipe(struct perf_data_file *file) · line 44

Quoted 10 lines
tools/perf/util/data.c +4 -2
 
 	if (!stat(file->path, &st) && st.st_size) {
-		/* TODO check errors properly */
 		char oldname[PATH_MAX];
-		snprintf(oldname, sizeof(oldname), "%s.old",
+		ret = snprintf(oldname, sizeof(oldname), "%s.old",
 			 file->path);
+		if (ret < 0) {
+			pr_err("failed to make name %s.old\n", file->path);
+			return -1;

Why -1? See what error code is typically used for these functions and return that.

julia

Quoted 22 lines
tools/perf/util/data.c +9 -1
+		}
+
 		unlink(oldname);
-		rename(file->path, oldname);
+
+		ret = rename(file->path, oldname);
+		if (ret < 0) {
+			pr_err("failed to rename %s to %s\n", file->path,
+				oldname);
+			return -1;
+		}
 	}
 
 	return 0;
Signature · 6 lines
2.4.2

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html