Skip to contentSkip to conversation

selftest: sync: Use common error handling code in sync_file_info()

PATCH In review SF Markus Elfring 2 participants 3 messages Nov 20, 2017 – Nov 21, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftest: sync: Use common error handling code in sync_file_info()

From: Markus Elfring <[email protected]>
Date: Mon, 20 Nov 2017 22:20:37 +0100

Add a jump target so that a bit of exception handling can be better reused at the end of this function.

This issue was detected by using the Coccinelle software.

---
 tools/testing/selftests/sync/sync.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
tools/testing/selftests/sync/sync.c +9 -10
index f3d599f249b9..d0d55377f6f8 100644
@@ -92,10 +92,8 @@ static struct sync_file_info *sync_file_info(int fd)
 		return NULL;
 
 	err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
-	if (err < 0) {
-		free(info);
-		return NULL;
-	}
+	if (err < 0)
+		goto free_info;
 
 	num_fences = info->num_fences;
 
@@ -104,22 +102,23 @@ static struct sync_file_info *sync_file_info(int fd)
 		info->num_fences = num_fences;
 
 		fence_info = calloc(num_fences, sizeof(*fence_info));
-		if (!fence_info) {
-			free(info);
-			return NULL;
-		}
+		if (!fence_info)
+			goto free_info;
 
 		info->sync_fence_info = (uint64_t)fence_info;
 
 		err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
 		if (err < 0) {
 			free(fence_info);
-			free(info);
-			return NULL;
+			goto free_info;
 		}
 	}
 
 	return info;
+
+free_info:
+	free(info);
+	return NULL;
 }
 
 static void sync_file_info_free(struct sync_file_info *info)
Signature · 1 line
2.15.0
maintainer
#1 lore raw
Quoted 7 lines · SF Markus Elfring

From: Markus Elfring <[email protected]>
Date: Mon, 20 Nov 2017 22:20:37 +0100

Add a jump target so that a bit of exception handling can be better reused at the end of this function.

This issue was detected by using the Coccinelle software.

I would like to see the Coccinelle log included in the commit log

Quoted 29 lines
---
 tools/testing/selftests/sync/sync.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
tools/testing/selftests/sync/sync.c +2 -6
index f3d599f249b9..d0d55377f6f8 100644
@@ -92,10 +92,8 @@ static struct sync_file_info *sync_file_info(int fd)
 		return NULL;
 
 	err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
-	if (err < 0) {
-		free(info);
-		return NULL;
-	}
+	if (err < 0)
+		goto free_info;
 
 	num_fences = info->num_fences;
 
@@ -104,22 +102,23 @@ static struct sync_file_info *sync_file_info(int fd)
 		info->num_fences = num_fences;
 
 		fence_info = calloc(num_fences, sizeof(*fence_info));
-		if (!fence_info) {
-			free(info);

tools/testing/selftests/sync/sync.c static struct sync_file_info *sync_file_info(int fd) · line 112

Quoted 10 lines
tools/testing/selftests/sync/sync.c +2 -2
-			return NULL;
-		}
+		if (!fence_info)
+			goto free_info;
 
 		info->sync_fence_info = (uint64_t)fence_info;
 
 		err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
 		if (err < 0) {
 			free(fence_info);

Why not add a free_fence_info to make it consistent?

Quoted 5 lines
tools/testing/selftests/sync/sync.c +1 -2
-			free(info);
-			return NULL;
+			goto free_info;
 		}

tools/testing/selftests/sync/sync.c static struct sync_file_info *sync_file_info(int fd) · line 124

Quoted 10 lines
tools/testing/selftests/sync/sync.c +4 -0
 	}
 
 	return info;
+
+free_info:
+	free(info);
+	return NULL;
 }
 
 static void sync_file_info_free(struct sync_file_info *info)

thanks,
-- Shuah

#2 lore raw
Quoted 6 lines
Quoted 4 lines

Add a jump target so that a bit of exception handling can be better reused at the end of this function.

This issue was detected by using the Coccinelle software.

I would like to see the Coccinelle log included in the commit log

I guess that I can not append the kind of report you might be looking for so far. This small update suggestion is just another result from one of my evolving scripts for the semantic patch language.

It was discussed under the topic “Comparing statement lists with SmPL” to some degree.
https://systeme.lip6.fr/pipermail/cocci/2017-August/004388.html

Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/sync/sync.c +2 -4
@@ -104,22 +102,23 @@ static struct sync_file_info *sync_file_info(int fd)
 		info->num_fences = num_fences;
 
 		fence_info = calloc(num_fences, sizeof(*fence_info));
-		if (!fence_info) {
-			free(info);
-			return NULL;
-		}
+		if (!fence_info)
+			goto free_info;
 
Quoted 7 lines

tools/testing/selftests/sync/sync.c static struct sync_file_info *sync_file_info(int fd) · line 112

Quoted 5 lines
tools/testing/selftests/sync/sync.c +0 -0
 		info->sync_fence_info = (uint64_t)fence_info;
 
 		err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
 		if (err < 0) {
 			free(fence_info);

Why not add a free_fence_info to make it consistent?

It did not add another jump label because it would be used only once for this if branch (according to your enquiry).

Quoted 3 lines

tools/testing/selftests/sync/sync.c static struct sync_file_info *sync_file_info(int fd) · line 96

Quoted 3 lines
tools/testing/selftests/sync/sync.c +1 -2
-			free(info);
-			return NULL;
+			goto free_info;

Do you expect a second update approach for this source code place?

Regards,
Markus