Opened 5 months ago

Closed 2 months ago

#14730 closed bug (fixed)

towlower() and towupper() should check the character range when gLocaleBackend is NULL.

Reported by: korli Owned by: nobody
Priority: normal Milestone: Unscheduled
Component: System/POSIX Version: R1/Development
Keywords: Cc:
Blocked By: Blocking:
Has a Patch: no Platform: All

Description

tolower and toupper are only valid for unsigned char types. see https://github.com/haikuports/haikuports/issues/3410 for reference.

Something like this:

wint_t
towlower(wint_t wc)
{
	if (gLocaleBackend == NULL) {
                if (wc < 0 || wc > 127)
			return 0;
		return tolower(wc);
        }
	wint_t result = wc;
	gLocaleBackend->ToWCTrans(wc, _ISlower, result);

	return result;
}


wint_t
towupper(wint_t wc)
{
	if (gLocaleBackend == NULL) {
                if (wc < 0 || wc > 127)
			return 0;
		return toupper(wc);
        }
	wint_t result = wc;
	gLocaleBackend->ToWCTrans(wc, _ISupper, result);

	return result;
}

Change History (3)

comment:1 Changed 5 months ago by pulkomandy

It should return wc rather than returning 0, for out of range characters.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/towlower.html

Upon successful completion, functions shall return the lowercase letter corresponding to the argument passed; otherwise, they shall return the argument unchanged.

Current implementation: https://git.haiku-os.org/haiku/tree/src/system/libroot/posix/locale/wctype.cpp#n124

It would be nice if tolower/toupper did not crash for out of range characters, however. Maybe we should put the range check there instead? However I see tolower/toupper are gcc builtins, so it's possible it knows the expected input ranges and will optimize away attempts to check it (already happened to us in other cases, leading to disabling the tree-vrp optimization back then).

comment:2 Changed 5 months ago by korli

+1 for returning wc.

comment:3 Changed 2 months ago by korli

Resolution: fixed
Status: newclosed

fixed in hrev52616.

Note: See TracTickets for help on using tickets.