http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49953

           Summary: _toupper() and _tolower() macros in ctype.h are broken
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: jmich...@yahoo.com


not sure if I categorized this correctly.
in ctype.h I found
#define _tolower(_Char) ((_Char)-'A'+'a')
#define _toupper(_Char) ((_Char)-'a'+'A')

this code is broken when it is used in the situations where the character is
already in the desired case.  then you get garbage characters.
try it yourself.
example: _tolower('a') produces garbage.


#include <ctype.h>
#include <stdio.h>
int main(void) {
    printf("%c%c", _toupper('A'),_tolower('a')); 
    //result is !ΓΌ which is not proper
    return 0;
}

a proper bug fix would be

#define _tolower(_Char) ((_Char>='A'&&_Char<='Z')?_Char+('a'-'A'):_Char)
#define _toupper(_Char) ((_Char>='a'&&_Char<='z')?_Char-('a'-'A'):_Char)


//#include <ctype.h>
#include <stdio.h>
#define _tolower(_Char) ((_Char>='A'&&_Char<='Z')?_Char+('a'-'A'):_Char)
#define _toupper(_Char) ((_Char>='a'&&_Char<='z')?_Char-('a'-'A'):_Char)
int main(void) {
    printf("%c%c%c%c", _toupper('A'),_toupper('a'),
_tolower('A'),_tolower('a'));
    //result is AAaa
    return 0;
}

Reply via email to