Skip to content

Commit 4a7d33e

Browse files
Pull up following revision(s) (requested by riastradh in ticket #59):
lib/libc/gen/ctype.3: revision 1.34 lib/libc/gen/ctype.3: revision 1.35 ctype(3): Clarify test program output. Update compiler warning. Should be a little easier to read. The fact that isprint returns _any_ nonzero value, not necessarily 1, isn't germane to the point here, so showing it return 5 or 2 doesn't really help to illustrate anything. ctype(3): Fix versions and clarify what LIBC_ALLOWCTYPEABUSE does. Both the extra diagnostics _and_ LIBC_ALLOWCTYPEABUSE are new in 11. The extra diagnostics were not added in 10 (unless someone went ahead and pulled them up while I wasn't looking!). LIBC_ALLOWCTYPEABUSE doesn't guarantee that the program won't crash; it just makes that depend on factors such as address space layout randomization -- where the ctype tables appear in memory relative to non-readable pages. PR lib/58208: ctype(3) provides poor runtime feedback of abuse
1 parent 47d4aac commit 4a7d33e

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

lib/libc/gen/ctype.3

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $NetBSD: ctype.3,v 1.31.14.1 2025/10/01 17:41:15 martin Exp $
1+
.\" $NetBSD: ctype.3,v 1.31.14.2 2025/10/19 10:21:18 martin Exp $
22
.\"
33
.\" Copyright (c) 1991 Regents of the University of California.
44
.\" All rights reserved.
@@ -62,18 +62,18 @@ See the specific manual pages for information about the
6262
test or conversion performed by each function.
6363
.Sh ENVIRONMENT
6464
In
65-
.Nx 10 ,
65+
.Nx 11 ,
6666
the
6767
.Nm
68-
functions will crash with a signal on certain invalid inputs as a
69-
diagnostic aid for applications; see
68+
functions will always crash with a signal on certain invalid inputs as
69+
a diagnostic aid for applications; see
7070
.Sx CAVEATS .
71-
In
72-
.Nx 11 ,
73-
setting the environment variable
71+
Setting the environment variable
7472
.Ev LIBC_ALLOWCTYPEABUSE
75-
before starting a program will make the functions return nonsense
76-
answers instead of crashing.
73+
before starting a program will restore the old behavior of returning
74+
nonsense answers for these inputs, or sometimes but not always
75+
crashing, depending on factors such as address space layout
76+
randomization.
7777
.Sh EXAMPLES
7878
To print an upper-case version of a string to stdout,
7979
the following code can be used:
@@ -173,9 +173,13 @@ main(int argc, char **argv)
173173
if (argc < 2)
174174
return 1;
175175
setlocale(LC_ALL, "");
176-
printf("%d %d\en", *argv[1], isprint(*argv[1]));
177-
printf("%d %d\en", (int)(unsigned char)*argv[1],
178-
isprint((unsigned char)*argv[1]));
176+
177+
printf(" char=%-4d isprint? %d\en",
178+
(int)*argv[1],
179+
isprint(*argv[1]) ? 1 : 0);
180+
printf("u_char=%-4d isprint? %d\en",
181+
(int)(unsigned char)*argv[1],
182+
isprint((unsigned char)*argv[1]) ? 1 : 0);
179183
return 0;
180184
}
181185
.Ed
@@ -185,24 +189,28 @@ passes
185189
.Vt char .
186190
At runtime, you may get nonsense answers for some inputs without the
187191
cast \(em if you're lucky and it doesn't crash:
188-
.Bd -literal -offset indent
192+
.Bd -literal
189193
% gcc -Wall -o test test.c
194+
In file included from /usr/include/ctype.h:100,
195+
from test.c:1:
190196
test.c: In function 'main':
191-
test.c:12:2: warning: array subscript has type 'char'
197+
test.c:15:21: warning: array subscript has type 'char' [-Wchar-subscripts]
198+
15 | isprint(*argv[1]) ? 1 : 0);
199+
| ^
192200
% LC_CTYPE=C ./test $(printf '\e270')
193-
-72 5
194-
184 0
201+
char=-72 isprint? 1
202+
u_char=184 isprint? 0
195203
% LC_CTYPE=C ./test $(printf '\e377')
196-
-1 0
197-
255 0
204+
char=-1 isprint? 0
205+
u_char=255 isprint? 0
198206
% LC_CTYPE=fr_FR.ISO8859-1 ./test $(printf '\e377')
199-
-1 0
200-
255 2
207+
char=-1 isprint? 0
208+
u_char=255 isprint? 1
201209
.Ed
202210
.Pp
203-
Some implementations of libc, such as glibc as of 2018, attempt to
204-
avoid the worst of the undefined behavior by defining the functions to
205-
work for all integer inputs representable by either
211+
Some implementations of libc, such as glibc as of 2018, hide the
212+
undefined behavior by defining the functions to work for all integer
213+
inputs representable by either
206214
.Vt unsigned char
207215
or
208216
.Vt char ,

0 commit comments

Comments
 (0)