Bug 23003 - isdigit() et al. return value inconsistent between function and macro
Summary: isdigit() et al. return value inconsistent between function and macro
Status: UNCONFIRMED
Alias: None
Product: glibc
Classification: Unclassified
Component: locale (show other bugs)
Version: 2.24
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-03-26 16:54 UTC by Martin Sebor
Modified: 2018-04-05 06:50 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2018-03-26 16:54:42 UTC
The C standard specifies the following for isdigit() and the other character classification functions:

  The functions in this subclause return nonzero (true) if and only if the value of the argument c conforms to that in the description of the function.

Although Glibc strictly conforms to this requirement, it returns a different non-zero value from the function than from the corresponding macro.  I think returning the same value from both would improve the quality of the implementation.

In addition, returning 1 from both instead of a seemingly arbitrary non-zero value would prevent bugs like those discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82272.  (See WG14 paper N2229 for a proposal to help detect these bugs.)

#include <assert.h>
#include <ctype.h>
#include <stdio.h>

int main (void)
{
  int i = isdigit ('1');
  int j = (isdigit)('1');
  printf ("%i %i\n", i, j);

  assert (isdigit ('1') == (isdigit)('1'));
  
  return 0;
}
2048 1
tst-isdigit.c:12: main: Assertion `isdigit ('1') == (isdigit)('1')' failed.
Comment 1 Andreas Schwab 2018-03-26 17:40:24 UTC
> (See WG14 paper N2229 for a proposal to help detect these bugs.)

What does it say?
Comment 2 Martin Sebor 2018-03-27 01:58:28 UTC
N2229 just published today:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2229.htm
Comment 3 Florian Weimer 2018-04-05 06:50:09 UTC
The same thing can happen with memcmp due to compiler intrinsics.  I'm not sure if fixing isdigit specifically is warranted.