[PATCH] tools/nolibc: avoid using plain integer as NULL pointer

Thomas Weißschuh posted 1 patch 1 month, 1 week ago
tools/include/nolibc/getopt.h     | 2 +-
tools/include/nolibc/sys.h        | 2 +-
tools/include/nolibc/sys/reboot.h | 2 +-
tools/include/nolibc/unistd.h     | 6 +++---
4 files changed, 6 insertions(+), 6 deletions(-)
[PATCH] tools/nolibc: avoid using plain integer as NULL pointer
Posted by Thomas Weißschuh 1 month, 1 week ago
The integer zero should not be used as NULL pointer.
It is invalid and sparse will complain about it.

Use proper NULL pointers instead.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/r/202509261452.g5peaXCc-lkp@intel.com/
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/include/nolibc/getopt.h     | 2 +-
 tools/include/nolibc/sys.h        | 2 +-
 tools/include/nolibc/sys/reboot.h | 2 +-
 tools/include/nolibc/unistd.h     | 6 +++---
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/tools/include/nolibc/getopt.h b/tools/include/nolibc/getopt.h
index 217abb95264b..87565e3b6a33 100644
--- a/tools/include/nolibc/getopt.h
+++ b/tools/include/nolibc/getopt.h
@@ -78,7 +78,7 @@ int getopt(int argc, char * const argv[], const char *optstring)
 		return '?';
 	}
 	if (optstring[i] == ':') {
-		optarg = 0;
+		optarg = NULL;
 		if (optstring[i + 1] != ':' || __optpos) {
 			optarg = argv[optind++];
 			if (__optpos)
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 28481feedb37..fcc36cffad4d 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -106,7 +106,7 @@ static __attribute__((unused))
 void *sbrk(intptr_t inc)
 {
 	/* first call to find current end */
-	void *ret = sys_brk(0);
+	void *ret = sys_brk(NULL);
 
 	if (ret && sys_brk(ret + inc) == ret + inc)
 		return ret + inc;
diff --git a/tools/include/nolibc/sys/reboot.h b/tools/include/nolibc/sys/reboot.h
index 4a1e435be669..38274c64a722 100644
--- a/tools/include/nolibc/sys/reboot.h
+++ b/tools/include/nolibc/sys/reboot.h
@@ -28,7 +28,7 @@ ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
 static __attribute__((unused))
 int reboot(int cmd)
 {
-	return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0));
+	return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, NULL));
 }
 
 #endif /* _NOLIBC_SYS_REBOOT_H */
diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h
index 7405fa2b89ba..bb5e80f3f05d 100644
--- a/tools/include/nolibc/unistd.h
+++ b/tools/include/nolibc/unistd.h
@@ -54,7 +54,7 @@ int msleep(unsigned int msecs)
 {
 	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
 
-	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
+	if (sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
 		return (my_timeval.tv_sec * 1000) +
 			(my_timeval.tv_usec / 1000) +
 			!!(my_timeval.tv_usec % 1000);
@@ -67,7 +67,7 @@ unsigned int sleep(unsigned int seconds)
 {
 	struct timeval my_timeval = { seconds, 0 };
 
-	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
+	if (sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
 		return my_timeval.tv_sec + !!my_timeval.tv_usec;
 	else
 		return 0;
@@ -78,7 +78,7 @@ int usleep(unsigned int usecs)
 {
 	struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
 
-	return sys_select(0, 0, 0, 0, &my_timeval);
+	return sys_select(0, NULL, NULL, NULL, &my_timeval);
 }
 
 static __attribute__((unused))

---
base-commit: 107eb8336e8782ae8e98b60962852a1e29aca715
change-id: 20251027-nolibc-sparse-c14fe0619edc

Best regards,
-- 
Thomas Weißschuh <linux@weissschuh.net>

Re: [PATCH] tools/nolibc: avoid using plain integer as NULL pointer
Posted by Willy Tarreau 1 month, 1 week ago
Hi Thomas,

On Sun, Nov 09, 2025 at 08:27:29PM +0100, Thomas Weißschuh wrote:
> The integer zero should not be used as NULL pointer.
> It is invalid and sparse will complain about it.
> 
> Use proper NULL pointers instead.

Huh ? I've been using that for decades and seeing it everywhere. That's
quite bizarre, 0 is perfectly valid as a pointer. Ah, here it is, in
C99:6.3.2.3:

  An integer constant expression with the value 0, or such an expression
  cast to type void *, is called a null pointer constant.

The change is small, so if it hurts sparse, I'm fine with changing it,
but at least the commit message should be adjusted to say that sparse
doesn't like it and not that it's not valid.

> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/r/202509261452.g5peaXCc-lkp@intel.com/
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

For the patch: Acked-by: Willy Tarreau <w@1wt.eu>

Thanks,
Willy
Re: [PATCH] tools/nolibc: avoid using plain integer as NULL pointer
Posted by David Laight 1 month, 1 week ago
On Sun, 9 Nov 2025 20:57:54 +0100
Willy Tarreau <w@1wt.eu> wrote:

> Hi Thomas,
> 
> On Sun, Nov 09, 2025 at 08:27:29PM +0100, Thomas Weißschuh wrote:
> > The integer zero should not be used as NULL pointer.
> > It is invalid and sparse will complain about it.
> > 
> > Use proper NULL pointers instead.  
> 
> Huh ? I've been using that for decades and seeing it everywhere. That's
> quite bizarre, 0 is perfectly valid as a pointer. Ah, here it is, in
> C99:6.3.2.3:
> 
>   An integer constant expression with the value 0, or such an expression
>   cast to type void *, is called a null pointer constant.

Note also that the above is true even if the value stored in memory for
a NULL point isn't the 'all zero' bit pattern.

The only time it actually matters whether you use 0 or NULL is in varargs
function calls.
Even that is dependant on the calling convention (when NULL is zero).

	David
Re: [PATCH] tools/nolibc: avoid using plain integer as NULL pointer
Posted by Thomas Weißschuh 1 month, 1 week ago
On 2025-11-09 20:57:54+0100, Willy Tarreau wrote:
> Hi Thomas,
> 
> On Sun, Nov 09, 2025 at 08:27:29PM +0100, Thomas Weißschuh wrote:
> > The integer zero should not be used as NULL pointer.
> > It is invalid and sparse will complain about it.
> > 
> > Use proper NULL pointers instead.
> 
> Huh ? I've been using that for decades and seeing it everywhere. That's
> quite bizarre, 0 is perfectly valid as a pointer. Ah, here it is, in
> C99:6.3.2.3:
> 
>   An integer constant expression with the value 0, or such an expression
>   cast to type void *, is called a null pointer constant.
> 
> The change is small, so if it hurts sparse, I'm fine with changing it,
> but at least the commit message should be adjusted to say that sparse
> doesn't like it and not that it's not valid.

Yeah, indeed '0' is special and valid, even in C23.
My recollection was wrong. Thanks for the hint!

> > Reported-by: kernel test robot <lkp@intel.com>
> > Closes: https://lore.kernel.org/r/202509261452.g5peaXCc-lkp@intel.com/
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> 
> For the patch: Acked-by: Willy Tarreau <w@1wt.eu>

And thanks again.


Thomas