Wrong type of argument for isdigit, toupper, and probably more #518

Closed
michaelrsweet opened this Issue Jan 13, 2004 · 2 comments

Comments

Projects
None yet
1 participant
Collaborator

michaelrsweet commented Jan 13, 2004

Version: 1.1-current
CUPS.org User: terra.gnome

I was looking at cupsLangGet for other reasons when I saw the comment

/*

  • Set the locale back to POSIX while we do string ops, since
  • apparently some buggy C libraries break ctype() for non-I18N
  • chars...
    */

I think I know what causes the observed effect: incorrect usage of
the ctype functions.

The ctype functions (isdigit, toupper, ...) are defined for all
integers that represent unsigned characters (i.e., 0-255) and
for the special value EOF (typically -1). The only exception to
this is isascii which is defined for all integers.

cupsLangGet uses both functions with arguments of type "char"
which gets cast to an integer in the range -128 to +127.

The correct usage is something like...

if (isdigit((unsigned char)(line[0])))

...even if that is incredible ugly.

GNU libc has a partial safety-net for this so Linux people will only
in extremely rare situation see any problem. Solaris and HPUX on the
other hand will access random non-owned memory if passed the wrong
kind.

Collaborator

michaelrsweet commented Jan 20, 2004

CUPS.org User: mike

Actually, while the issue of making sure the chars are between -1 and 255 is valid, setlocale() does change the ctype tables in interesting ways since not all locales will treat T and t as the same letter with different case (thus the "LC_CTYPE" category...)

That said, we can certainly look at the calls to the ctype functions/macros to ensure that they are "safe" from the standpoint of only passing in the limited range of values they support...

Collaborator

michaelrsweet commented Feb 25, 2004

CUPS.org User: mike

Fixed in CVS - the anonymous CVS repository will be updated at midnight EST.

michaelrsweet added this to the Stable milestone Mar 17, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment