Skip to contentSkip to conversation

PATCH proposal removing use of strtoul in events/evgpeinit.c

Devel Richard PALO 2 participants 10 messages Sep 4, 2015 – Sep 11, 2015 ACPI component architecture (acpica)

lore
Share & tools
lore

Richard PALO

author
#0 lore raw

I'm noticing on catching up recently, e.g. on 20150717, that there is the following issue:

Quoted 2 lines

/home/richard/ws/illumos-gate/usr/src/common/acpica/components/events/evgpeinit.c: In function 'AcpiEvMatchGpeMethod':
/home/richard/ws/illumos-gate/usr/src/common/acpica/components/events/evgpeinit.c:398: error: implicit declaration of function 'strtoul' [-Wimplicit-function-declaration]

strtoul is indirectly defined in acsolaris via a system header as:

I like to propose the following patch substituting for a simplified, specific conversion in the file events/evgpeinit.c.

Quoted 1 line

extern unsigned long strtoul(const char *, char **, int);

This seems to be the only real use of strtoul with _KERNEL defined.

Looking at the code in question, for two hex digits is doesn't seem the worth to use it so I propose to replace as follows:

Quoted 29 lines
usr/src/common/acpica/components/events/evgpeinit.c +16 -2
index d23a345..de1e15a 100644
@@ -395,8 +395,22 @@ AcpiEvMatchGpeMethod (
 
     /* 4) The last two characters of the name are the hex GPE Number */
 
-    GpeNumber = strtoul (&Name[2], NULL, 16);
-    if (GpeNumber == ACPI_UINT32_MAX)
+    /* GpeNumber = strtoul (&Name[2], NULL, 16); */
+    /*
+     * As this function can be called in KERNEL, not all systems have
+     * strtoul() available.  Since this is only for a single hex value,
+     * manually convert nibble by nibble, checking for possible errors.
+     */
+    GpeNumber = isdigit(Name[2]) ? (Name[2] - '0')<<4 :
+       isxdigit(Name[2]) ? (toupper(Name[2]) - 'A' + 10)<<4 : 0xFFFF;
+
+    if (GpeNumber != 0xFFFF) {
+        GpeNumber = isdigit(Name[3]) ? GpeNumber + (Name[3] - '0') :
+           isxdigit(Name[3]) ? GpeNumber + (toupper(Name[3]) - 'A' + 10) :
+           0xFFFF;
+    }
+
+    if (GpeNumber == 0xFFFF || Name[4] != '\0')
     {
         /* Conversion failed; invalid method, just ignore it */
 
Signature · 1 line
Richard PALO
#1 lore raw

Sure, but the original code is much simpler.

Quoted 66 lines

-----Original Message-----
From: Devel [mailto:devel-bounces(a)acpica.org] On Behalf Of Richard PALO
Sent: Friday, September 04, 2015 4:12 AM
To: devel(a)acpica.org
Subject: [Devel] PATCH proposal removing use of strtoul in events/evgpeinit.c

I'm noticing on catching up recently, e.g. on 20150717, that there is the following issue:

Quoted 1 line

/home/richard/ws/illumos-

gate/usr/src/common/acpica/components/events/evgpeinit.c: In function 'AcpiEvMatchGpeMethod':

Quoted 3 lines

/home/richard/ws/illumos-gate/usr/src/common/acpica/components/events/ evgpeinit.c:398: error: implicit declaration of function 'strtoul' [-Wimplicit-function-declaration]

strtoul is indirectly defined in acsolaris via a system header as:

I like to propose the following patch substituting for a simplified, specific conversion in the file events/evgpeinit.c.

Quoted 1 line

extern unsigned long strtoul(const char *, char **, int);

This seems to be the only real use of strtoul with _KERNEL defined.

Looking at the code in question, for two hex digits is doesn't seem the worth to use it so I propose to replace as follows:

Quoted 14 lines
usr/src/common/acpica/components/events/evgpeinit.c +2 -2
index d23a345..de1e15a 100644
@@ -395,8 +395,22 @@ AcpiEvMatchGpeMethod (
 
     /* 4) The last two characters of the name are the hex GPE Number
 */
 
-    GpeNumber = strtoul (&Name[2], NULL, 16);
-    if (GpeNumber == ACPI_UINT32_MAX)
+    /* GpeNumber = strtoul (&Name[2], NULL, 16); */
+    /*

usr/src/common/acpica/components/events/evgpeinit.c AcpiEvMatchGpeMethod ( · line 410

Quoted 10 lines
usr/src/common/acpica/components/events/evgpeinit.c +10 -0
+     * As this function can be called in KERNEL, not all systems have
+     * strtoul() available.  Since this is only for a single hex value,
+     * manually convert nibble by nibble, checking for possible errors.
+     */
+    GpeNumber = isdigit(Name[2]) ? (Name[2] - '0')<<4 :
+       isxdigit(Name[2]) ? (toupper(Name[2]) - 'A' + 10)<<4 : 0xFFFF;
+
+    if (GpeNumber != 0xFFFF) {
+        GpeNumber = isdigit(Name[3]) ? GpeNumber + (Name[3] - '0') :
+           isxdigit(Name[3]) ? GpeNumber + (toupper(Name[3]) - 'A' +

10) :

usr/src/common/acpica/components/events/evgpeinit.c AcpiEvMatchGpeMethod ( · line 416

Quoted 7 lines
usr/src/common/acpica/components/events/evgpeinit.c +4 -0
+           0xFFFF;
+    }
+
+    if (GpeNumber == 0xFFFF || Name[4] != '\0')
     {
         /* Conversion failed; invalid method, just ignore it */

--
Richard PALO

_______________________________________________
Devel mailing list
Devel(a)acpica.org
https://lists.acpica.org/mailman/listinfo/devel

author
#2 lore raw
Quoted 2 lines · Robert

Sure, but the original code is much simpler.

The reason for the error was that, by default, the extern declaration was forcluded by _DDI_STRICT being defined..
which is to say that strtoul, strictly speaking is unavailable in the Solaris kernel (excepting kmdb).

So I kludged and added to acsolaris.h #ifdef _KERNEL section :

Quoted 13 lines

static __GNU_INLINE unsigned long
strtoul(const char *str, char **nptr, int base)

{
	unsigned long ret;
	int status;

	if ((status = ddi_strtoul(str, nptr, base, &ret)) != 0) {
		if (status == ERANGE)
			return (ULONG_MAX);
		return (0);
	}
	return (ret);
}

Still seems for one call in the kernel to convert a single HEX byte from ascii to unsigned long, that the portable solution is to avoid using 'strtoul' in paths operating in kernel space. (also considering the Linux discussion also around strtoul in the kernel)

cheers,

Signature · 1 line
Richard PALO
#3 lore raw

This is hardly the only use of strtoul in ACPICA. May or may not be an issue on solaris, depending on the kernel/ACPICA configuration.

Quoted 38 lines

-----Original Message-----
From: Richard PALO [mailto:richard(a)netbsd.org]
Sent: Friday, September 04, 2015 11:02 PM
To: Moore, Robert; devel(a)acpica.org
Subject: Re: [Devel] PATCH proposal removing use of strtoul in events/evgpeinit.c

Quoted 2 lines · Robert

Sure, but the original code is much simpler.

The reason for the error was that, by default, the extern declaration was forcluded by _DDI_STRICT being defined..
which is to say that strtoul, strictly speaking is unavailable in the Solaris kernel (excepting kmdb).

So I kludged and added to acsolaris.h #ifdef _KERNEL section :

Quoted 12 lines

static __GNU_INLINE unsigned long

strtoul(const char *str, char **nptr, int base) {
	unsigned long ret;
	int status;

	if ((status = ddi_strtoul(str, nptr, base, &ret)) != 0) {
		if (status == ERANGE)
			return (ULONG_MAX);
		return (0);
	}
	return (ret);
}

Still seems for one call in the kernel to convert a single HEX byte from ascii to unsigned long, that the portable solution is to avoid using 'strtoul' in paths operating in kernel space.
(also considering the Linux discussion also around strtoul in the kernel)

cheers,
--
Richard PALO

#4 lore raw
In reply to Richard PALO

I've got a better question:

Quoted 2 lines

which is to say that strtoul, strictly speaking is unavailable in the Solaris kernel (excepting kmdb).

Why not?

Quoted 38 lines

-----Original Message-----
From: Richard PALO [mailto:richard(a)netbsd.org]
Sent: Friday, September 04, 2015 11:02 PM
To: Moore, Robert; devel(a)acpica.org
Subject: Re: [Devel] PATCH proposal removing use of strtoul in events/evgpeinit.c

Quoted 2 lines · Robert

Sure, but the original code is much simpler.

The reason for the error was that, by default, the extern declaration was forcluded by _DDI_STRICT being defined..
which is to say that strtoul, strictly speaking is unavailable in the Solaris kernel (excepting kmdb).

So I kludged and added to acsolaris.h #ifdef _KERNEL section :

Quoted 12 lines

static __GNU_INLINE unsigned long

strtoul(const char *str, char **nptr, int base) {
	unsigned long ret;
	int status;

	if ((status = ddi_strtoul(str, nptr, base, &ret)) != 0) {
		if (status == ERANGE)
			return (ULONG_MAX);
		return (0);
	}
	return (ret);
}

Still seems for one call in the kernel to convert a single HEX byte from ascii to unsigned long, that the portable solution is to avoid using 'strtoul' in paths operating in kernel space.
(also considering the Linux discussion also around strtoul in the kernel)

cheers,
--
Richard PALO

author
#5 lore raw
Quoted 8 lines · Robert

I've got a better question:

Quoted 2 lines

which is to say that strtoul, strictly speaking is unavailable in the Solaris kernel (excepting kmdb).

Why not?

Given numerous other build problems involving ACPI_DISASSEMLER in _KERNEL, those are disabled for now
(issues with MpSave* which I believe other platforms are experiencing as well, just like the visibility of AcpiUtConvertStringToUuid).

I believe other uses of strtoul may tend to be concentrated there...

As to the history of decisions involving ddi_strtoul/strtoul, a veteran sun kernel developer needs to be asked...

In any event, currently the wrapper around ddi_strtoul seems reasonable enough.

BTW, have things been linted recently (we use sun studio lint)?

I had to add the following to acsolaris.h

Quoted 6 lines

#ifdef __SUNPRO_C
#pragma error_messages(off, E_STATIC_UNUSED) #pragma error_messages(off, E_FUNC_ARG_UNUSED) #pragma error_messages(off, E_END_OF_LOOP_CODE_NOT_REACHED) #pragma error_messages(off, E_FUNC_RET_MAYBE_IGNORED2) #endif

not to mention kludge needed when linting global crosschecks in the illumos gate, for the later

Quoted 4 lines

"/home/richard/src/illumos-gate/usr/src/common/acpica/components/dispatcher/dsobject.c", line 351: warning: function returns value which is sometimes ignored: memcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/dispatcher/dsutils.c", line 608: warning: function returns value which is sometimes ignored: strncpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/executer/exmisc.c", line 370: warning: function returns value which is sometimes ignored: strcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/executer/exnames.c", line 224: warning: function returns value which is sometimes ignored: strcat (E_FUNC_RET_MAYBE_IGNORED2)

Signature · 1 line
Richard PALO
#6 lore raw
Quoted 26 lines

-----Original Message-----
From: Richard PALO [mailto:richard(a)netbsd.org]
Sent: Friday, September 11, 2015 3:57 AM
To: Moore, Robert; devel(a)acpica.org
Subject: Re: [Devel] PATCH proposal removing use of strtoul in events/evgpeinit.c

Quoted 8 lines · Robert

I've got a better question:

Quoted 2 lines

which is to say that strtoul, strictly speaking is unavailable in the Solaris kernel (excepting kmdb).

Why not?

Given numerous other build problems involving ACPI_DISASSEMLER in _KERNEL, those are disabled for now (issues with MpSave* which I believe other platforms are experiencing as well, just like the visibility of AcpiUtConvertStringToUuid).

I believe other uses of strtoul may tend to be concentrated there...

Quoted 7 lines

As to the history of decisions involving ddi_strtoul/strtoul, a veteran sun kernel developer needs to be asked...

In any event, currently the wrapper around ddi_strtoul seems reasonable enough.

BTW, have things been linted recently (we use sun studio lint)?

Every lint is different, unfortunately. And there are lots of things that we need to ignore, which in itself depends on which lint. So, it is mostly useless for us.

Quoted 25 lines

I had to add the following to acsolaris.h

Quoted 5 lines

#ifdef __SUNPRO_C
#pragma error_messages(off, E_STATIC_UNUSED) #pragma error_messages(off, E_FUNC_ARG_UNUSED) #pragma error_messages(off, E_END_OF_LOOP_CODE_NOT_REACHED) #pragma error_messages(off, E_FUNC_RET_MAYBE_IGNORED2) #endif

not to mention kludge needed when linting global crosschecks in the illumos gate, for the later

Quoted 4 lines

"/home/richard/src/illumos-gate/usr/src/common/acpica/components/dispa tcher/dsobject.c", line 351: warning: function returns value which is sometimes ignored: memcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/dispa

Quoted 8 lines

tcher/dsutils.c", line 608: warning: function returns value which is sometimes ignored: strncpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/execu ter/exmisc.c", line 370: warning: function returns value which is sometimes ignored: strcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/execu ter/exnames.c", line 224: warning: function returns value which is sometimes ignored: strcat (E_FUNC_RET_MAYBE_IGNORED2)

--
Richard PALO

#7 lore raw
In reply to Richard PALO

Not to mention that every compiler is different, and you need to put your own "ignore error" types of things in your platform-specific or compiler-specific header. Such is the price of attempting to build OS- and compiler-independent code.

Quoted 72 lines

-----Original Message-----
From: Moore, Robert
Sent: Friday, September 11, 2015 7:54 AM
To: 'Richard PALO'; devel(a)acpica.org
Cc: Box, David E; Zheng, Lv
Subject: RE: [Devel] PATCH proposal removing use of strtoul in events/evgpeinit.c

Quoted 24 lines

-----Original Message-----
From: Richard PALO [mailto:richard(a)netbsd.org]
Sent: Friday, September 11, 2015 3:57 AM
To: Moore, Robert; devel(a)acpica.org
Subject: Re: [Devel] PATCH proposal removing use of strtoul in events/evgpeinit.c

Quoted 8 lines · Robert

I've got a better question:

Quoted 2 lines

which is to say that strtoul, strictly speaking is unavailable in the Solaris kernel (excepting kmdb).

Why not?

Given numerous other build problems involving ACPI_DISASSEMLER in _KERNEL, those are disabled for now (issues with MpSave* which I believe other platforms are experiencing as well, just like the visibility of AcpiUtConvertStringToUuid).

I believe other uses of strtoul may tend to be concentrated there...

Quoted 7 lines

As to the history of decisions involving ddi_strtoul/strtoul, a veteran sun kernel developer needs to be asked...

In any event, currently the wrapper around ddi_strtoul seems reasonable enough.

BTW, have things been linted recently (we use sun studio lint)?

Every lint is different, unfortunately. And there are lots of things that we need to ignore, which in itself depends on which lint. So, it is mostly useless for us.

Quoted 25 lines

I had to add the following to acsolaris.h

Quoted 5 lines

#ifdef __SUNPRO_C
#pragma error_messages(off, E_STATIC_UNUSED) #pragma error_messages(off, E_FUNC_ARG_UNUSED) #pragma error_messages(off, E_END_OF_LOOP_CODE_NOT_REACHED) #pragma error_messages(off, E_FUNC_RET_MAYBE_IGNORED2) #endif

not to mention kludge needed when linting global crosschecks in the illumos gate, for the later

Quoted 4 lines

"/home/richard/src/illumos-gate/usr/src/common/acpica/components/dis pa tcher/dsobject.c", line 351: warning: function returns value which is sometimes ignored: memcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/dis

Quoted 8 lines

pa tcher/dsutils.c", line 608: warning: function returns value which is sometimes ignored: strncpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/exe cu ter/exmisc.c", line 370: warning: function returns value which is sometimes ignored: strcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/exe cu ter/exnames.c", line 224: warning: function returns value which is sometimes ignored: strcat (E_FUNC_RET_MAYBE_IGNORED2)

--
Richard PALO

author
#8 lore raw
Quoted 18 lines · Robert

Not to mention that every compiler is different, and you need to put your own "ignore error" types of things in your platform-specific or compiler-specific header. Such is the price of attempting to build OS- and compiler-independent code.

Quoted 15 lines
Quoted 15 lines

not to mention kludge needed when linting global crosschecks in the illumos gate, for the later

Quoted 12 lines

"/home/richard/src/illumos-gate/usr/src/common/acpica/components/dis pa tcher/dsobject.c", line 351: warning: function returns value which is sometimes ignored: memcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/dis pa tcher/dsutils.c", line 608: warning: function returns value which is sometimes ignored: strncpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/exe cu ter/exmisc.c", line 370: warning: function returns value which is sometimes ignored: strcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/exe cu ter/exnames.c", line 224: warning: function returns value which is sometimes ignored: strcat (E_FUNC_RET_MAYBE_IGNORED2)

? Sorry, I guess I don't follow. for example, posix, and various c standards define memcpy as

Quoted 1 line

void *memcpy(void *restrict s1, const void *restrict s2, size_t n);

the first reference above, components/dispatcher/dsobject.c does:

Quoted 5 lines

349 if (ByteList)
350 {
351 memcpy (ObjDesc->Buffer.Pointer, ByteList->Named.Data,
352 ByteListLength);
353 }

what would make most static analyzers happy is a simple void cast -- (void) memcpy(...)

Then, at least, ignoring the return value is explicit instead of possibly just being forgotten..

This should also be the most portable form of C (at least for the cases at hand).

There are numerous examples of this sort of thing over and above what I listed above...

cheers,

Signature · 1 line
Richard PALO
#9 lore raw

Agreed

Quoted 43 lines

-----Original Message-----
From: Richard PALO [mailto:richard(a)netbsd.org]
Sent: Friday, September 11, 2015 8:36 AM
To: Moore, Robert; devel(a)acpica.org
Cc: Box, David E; Zheng, Lv
Subject: Re: [Devel] PATCH proposal removing use of strtoul in events/evgpeinit.c

Quoted 1 line · Robert

Not to mention that every compiler is different, and you need to put

your own "ignore error" types of things in your platform-specific or compiler-specific header. Such is the price of attempting to build OS- and compiler-independent code.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

not to mention kludge needed when linting global crosschecks in the illumos gate, for the later

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

"/home/richard/src/illumos-gate/usr/src/common/acpica/components/di s pa tcher/dsobject.c", line 351: warning: function returns value which is sometimes ignored: memcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/di s pa tcher/dsutils.c", line 608: warning: function returns value

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

which is sometimes ignored: strncpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/ex e cu ter/exmisc.c", line 370: warning: function returns value which is sometimes ignored: strcpy (E_FUNC_RET_MAYBE_IGNORED2) "/home/richard/src/illumos-gate/usr/src/common/acpica/components/ex e cu ter/exnames.c", line 224: warning: function returns value which is sometimes ignored: strcat (E_FUNC_RET_MAYBE_IGNORED2)

? Sorry, I guess I don't follow. for example, posix, and various c standards define memcpy as

Quoted 1 line

void *memcpy(void *restrict s1, const void *restrict s2, size_t n);

the first reference above, components/dispatcher/dsobject.c does:

Quoted 5 lines

349 if (ByteList)
350 {
351 memcpy (ObjDesc->Buffer.Pointer, ByteList->Named.Data,
352 ByteListLength);
353 }

Quoted 9 lines

what would make most static analyzers happy is a simple void cast -- (void) memcpy(...)

Then, at least, ignoring the return value is explicit instead of possibly just being forgotten..

This should also be the most portable form of C (at least for the cases at hand).

Agreed. Some of these have apparently slipped in, so we will put it on our list.

Quoted 6 lines

There are numerous examples of this sort of thing over and above what I listed above...

cheers,
--
Richard PALO