Skip to contentSkip to conversation

src/swapon.c: initiate p to NULL

PATCH Superseded Sun Ke 2 participants 3 messages Jan 28, 2021 – Feb 1, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sun Ke

author
#0 lore raw

PATCH 1/1 src/swapon.c: initiate p to NULL

when run make, show:
swapon.c:135:3: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]

   memcpy(p, buf, BUF_SIZE);
      ^~~~~~~~~~~~~~~~~~~~~~~~

fix it.

---
 src/swapon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/swapon.c +1 -1
index afaed405..fe8949a3 100644
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
 		.sa_handler	= handle_signal,
 	};
 	enum verbs verb = TEST_SWAPON;
-	void *p;
+	void *p = NULL;
 	ssize_t sz;
 	int fd = -1;
 	int ret, c;
Signature · 1 line
2.13.6
#1 lore raw
Quoted 6 lines · Sun Ke

when run make, show:
swapon.c:135:3: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]

   memcpy(p, buf, BUF_SIZE);
      ^~~~~~~~~~~~~~~~~~~~~~~~

fix it.

If you don't have a situation to prove this's a real bug, I guess the gcc might think the 'p' can be used uninitialized at here when verb is TEST_MWRITE_BEFORE_AND_MWRITE_AFTER:

        case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER:
                memcpy(p, buf, BUF_SIZE);
                break;

But from the code logic, if verb is TEST_MWRITE_BEFORE_AND_MWRITE_AFTER, the 'p' will be initialized by:

switch (verb) {
case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER:
case TEST_MWRITE_BEFORE:
p = mmap(NULL, BUF_SIZE, PROT_WRITE | PROT_READ, MAP_SHARED,

So I think this's a fake uninitialized warning (Maybe I'm wrong. But if I'm right, better to specify that in commit log).

Anyway, I personally don't object to initialize a pointer to NULL at beginning, and make a build warning disappear. Leave the decision to maintainer :)

Thanks,
Zorro

Quoted 22 lines
---
 src/swapon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/swapon.c +1 -1
index afaed405..fe8949a3 100644
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
 		.sa_handler	= handle_signal,
 	};
 	enum verbs verb = TEST_SWAPON;
-	void *p;
+	void *p = NULL;
 	ssize_t sz;
 	int fd = -1;
 	int ret, c;
Signature · 1 line
2.13.6
author
#2 lore raw

hi Zorro

Quoted 17 lines · Zorro Lang
Quoted 6 lines · Sun Ke

when run make, show:
swapon.c:135:3: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]

    memcpy(p, buf, BUF_SIZE);
       ^~~~~~~~~~~~~~~~~~~~~~~~

fix it.

If you don't have a situation to prove this's a real bug, I guess the gcc might think the 'p' can be used uninitialized at here when verb is TEST_MWRITE_BEFORE_AND_MWRITE_AFTER:

         case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER:
                 memcpy(p, buf, BUF_SIZE);
                 break;

But from the code logic, if verb is TEST_MWRITE_BEFORE_AND_MWRITE_AFTER, the 'p' will be initialized by:

Quoted 8 lines

switch (verb) {
case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER:
case TEST_MWRITE_BEFORE:
p = mmap(NULL, BUF_SIZE, PROT_WRITE | PROT_READ, MAP_SHARED,

So I think this's a fake uninitialized warning (Maybe I'm wrong. But if I'm right, better to specify that in commit log).

Yes, you are right.

Sorry, I did not analyse the code logic carefully.

Quoted 3 lines

Anyway, I personally don't object to initialize a pointer to NULL at beginning, and make a build warning disappear. Leave the decision to maintainer :)

I will improve the commit.

Quoted 3 lines

Thanks,
Zorro

Thanks,
Sun Ke

Quoted 23 lines
Quoted 22 lines
---
  src/swapon.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
src/swapon.c +1 -1
index afaed405..fe8949a3 100644
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
  		.sa_handler	= handle_signal,
  	};
  	enum verbs verb = TEST_SWAPON;
-	void *p;
+	void *p = NULL;
  	ssize_t sz;
  	int fd = -1;
  	int ret, c;
Signature · 1 line
2.13.6

.