Skip to contentSkip to conversation

tests/hd-geo-test: Don't pass NULL to unlink()

PATCH Applied Peter Maydell 3 participants 9 messages Aug 5, 2016 – Sep 8, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/hd-geo-test: Don't pass NULL to unlink()

The unlink() function doesn't accept a NULL pointer, so don't pass it one. Spotted by the clang sanitizer.

---
 tests/hd-geo-test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/hd-geo-test.c +3 -1
index 12ee392..6176e81 100644
@@ -416,7 +416,9 @@ int main(int argc, char **argv)
     ret = g_test_run();
 
     for (i = 0; i < backend_last; i++) {
-        unlink(img_file_name[i]);
+        if (img_file_name[i]) {
+            unlink(img_file_name[i]);
+        }
     }
 
     return ret;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 13 lines · Peter Maydell

The unlink() function doesn't accept a NULL pointer, so don't pass it one. Spotted by the clang sanitizer.

---
 tests/hd-geo-test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/hd-geo-test.c +0 -0
index 12ee392..6176e81 100644

tests/hd-geo-test.c int main(int argc, char **argv) · line 424

Quoted 10 lines
tests/hd-geo-test.c +3 -1
@@ -416,7 +416,9 @@ int main(int argc, char **argv)
     ret = g_test_run();
 
     for (i = 0; i < backend_last; i++) {
-        unlink(img_file_name[i]);
+        if (img_file_name[i]) {
+            unlink(img_file_name[i]);
+        }
     }
 
     return ret;

And what terrible, terrible things unlink()'s going to do when passed a null pointer? Turns out the same scary terrible thing it has always
done: return -1 and set errno = EFAULT.

#2 lore raw
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · Peter Maydell

The unlink() function doesn't accept a NULL pointer, so don't pass it one. Spotted by the clang sanitizer.

---
 tests/hd-geo-test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/hd-geo-test.c +3 -1
index 12ee392..6176e81 100644
@@ -416,7 +416,9 @@ int main(int argc, char **argv)
     ret = g_test_run();
 
     for (i = 0; i < backend_last; i++) {
-        unlink(img_file_name[i]);
+        if (img_file_name[i]) {
+            unlink(img_file_name[i]);
+        }
     }
 
Quoted 5 lines

tests/hd-geo-test.c int main(int argc, char **argv) · line 424

Quoted 1 line
tests/hd-geo-test.c +0 -0
     return ret;

And what terrible, terrible things unlink()'s going to do when passed a null pointer? Turns out the same scary terrible thing it has always
done: return -1 and set errno = EFAULT.

Feel free to send a bug report to the glibc folks saying they shouldn't have marked it as __nonnull((1)).

thanks
-- PMM

#3 lore raw
In reply to Peter Maydell

Ping?

thanks
-- PMM

Quoted 25 lines · Peter Maydell

The unlink() function doesn't accept a NULL pointer, so don't pass it one. Spotted by the clang sanitizer.

---
 tests/hd-geo-test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/hd-geo-test.c +3 -2
index 12ee392..6176e81 100644
@@ -416,7 +416,9 @@ int main(int argc, char **argv)
     ret = g_test_run();
 
     for (i = 0; i < backend_last; i++) {
-        unlink(img_file_name[i]);
+        if (img_file_name[i]) {
+            unlink(img_file_name[i]);
+        }
     }
 
     return ret;
--

2.7.4

#4 lore raw
Quoted 5 lines · Peter Maydell

Ping?

thanks
-- PMM

Not sure who this belongs to; I can queue it up alongside some IDE and FDC stuff in the future if you like.

--js

Quoted 28 lines · Peter Maydell
Quoted 25 lines

The unlink() function doesn't accept a NULL pointer, so don't pass it one. Spotted by the clang sanitizer.

---
 tests/hd-geo-test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/hd-geo-test.c +3 -2
index 12ee392..6176e81 100644
@@ -416,7 +416,9 @@ int main(int argc, char **argv)
     ret = g_test_run();
 
     for (i = 0; i < backend_last; i++) {
-        unlink(img_file_name[i]);
+        if (img_file_name[i]) {
+            unlink(img_file_name[i]);
+        }
     }
 
     return ret;
--

2.7.4

Signature · 1 line
—js
#5 lore raw
Quoted 6 lines · John Snow
Quoted 2 lines · Peter Maydell

Ping?

Not sure who this belongs to; I can queue it up alongside some IDE and FDC stuff in the future if you like.

I can apply it directly; I'd just like review :-)

thanks
-- PMM

Reviewed-by
#6 lore raw
Quoted 10 lines · Peter Maydell
Quoted 6 lines · John Snow
Quoted 2 lines · Peter Maydell

Ping?

Not sure who this belongs to; I can queue it up alongside some IDE and FDC stuff in the future if you like.

I can apply it directly; I'd just like review :-)

lol! yes, of course you can...

Quoted 3 lines

thanks
-- PMM

Well, I know some have religious arguments against making sanitizers happy, but that's not my religion.

If some systems appear to think that unlink must take a nonnull argument, I think that's a bug -- but making the sanitizer happy doesn't cost us much either IMO.

(And it's /just/ a test.)

So, personally:

#7 lore raw
Quoted 6 lines · John Snow

Well, I know some have religious arguments against making sanitizers happy, but that's not my religion.

If some systems appear to think that unlink must take a nonnull argument, I think that's a bug -- but making the sanitizer happy doesn't cost us much either IMO.

POSIX doesn't mandate that unlink() handles NULL, so it's just a quality-of-implementation issue in the library to handle it without barfing (or conversely a QoI issue to over-conservatively mark it as requires-nonnull in the system headers, if you like).

In any case my general view with all these sanitizing/lint tools is that it's usually worth taking a few unnecessary-looking detours in order to get to zero-warnings so you can easily flag up when new code triggers a warning (which could well be a bug).

Quoted 5 lines

(And it's /just/ a test.)

So, personally:

Thanks!

-- PMM

author Applied
#8 lore raw
In reply to Peter Maydell
Quoted 13 lines · Peter Maydell

The unlink() function doesn't accept a NULL pointer, so don't pass it one. Spotted by the clang sanitizer.

---
 tests/hd-geo-test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/hd-geo-test.c +0 -0
index 12ee392..6176e81 100644

tests/hd-geo-test.c int main(int argc, char **argv) · line 424

Quoted 10 lines
tests/hd-geo-test.c +3 -1
@@ -416,7 +416,9 @@ int main(int argc, char **argv)
     ret = g_test_run();
 
     for (i = 0; i < backend_last; i++) {
-        unlink(img_file_name[i]);
+        if (img_file_name[i]) {
+            unlink(img_file_name[i]);
+        }
     }
 
     return ret;

Applied to master, thanks.

-- PMM