Opened 5 years ago

Closed 5 years ago

Last modified 4 years 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: R1/beta2
Component: System/POSIX Version: R1/Development
Keywords: Cc:
Blocked By: Blocking:
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 (4)

comment:1 by pulkomandy, 5 years ago

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 by korli, 5 years ago

+1 for returning wc.

comment:3 by korli, 5 years ago

Resolution: fixed
Status: newclosed

fixed in hrev52616.

comment:4 by nielx, 4 years ago

Milestone: UnscheduledR1/beta2

Assign tickets with status=closed and resolution=fixed within the R1/beta2 development window to the R1/beta2 Milestone

Note: See TracTickets for help on using tickets.