Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Tcl_NumUtfChars(...) no overflow check

Closed / Fixed2738427fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 7 years ago
Descriptionplain2009-04-06 19:41:37
Tcl_NumUtfChars(unicode, -1) can return negative
counts if there are really more than INT_MAX Utf
chars located at unicode before a terminating \0
is seen.

In addition, there is another overflow problem, e.g.
Tcl_NumUtfChars("\xE2\x82\xAC", 2): This call returns
1 instead of the expected 2.
dgpplain2009-04-11 03:09:53
comment test....
jan.nijtmans Ticket [2738427fff]: 1 change [History diff]2014-02-21 19:07:44
jan.nijtmansplain2017-06-08 12:26:18
Problem with the Tcl_NumUtfChars("\xE2\x82\xAC", 2) call is that
the 3th byte is read, even though a length of only 2 bytes was
requested. Since with only 2 bytes, the unicode character is
incomplete, Tcl_NumUtfChars() should count this as 2 separate bytes.
jan.nijtmanswiki2017-06-08 12:41:17

Fixed in core-8-5-branch, core-8-6-branch and trunk.

For Tcl 9 it would be better to change the signature of Tcl_NumUtfChars (using size_t length parameter and return value in stead of int), but that's out-of-scope for this ticket.

dgpplain2017-06-08 12:54:47
Thanks for finding and addressing this old ticket.

I'm worried it's not quite a correct answer.

The overflow detection is only done after
the loop ends, and not at each pass through
the loop.  This leaves open the possibility
that i could circle all the way back to a
non-negative value before '\0' is seen and
we get a count that's (UINT_MAX + 1) too small.

Since the return type can only handle a value
up to INT_MAX, and since there's no error
reporting channel available, should this routine
just panic for inputs outside that supportable range?
jan.nijtmanswiki2017-06-08 13:45:29

Yes I'm aware that this is not 100% correct yet, in Tcl 9 we can do better. Panicing would be OK to me, it's not a use-case that will be encountered in real life .....

Thank you, Don, for your feedback!

dgpplain2017-06-08 14:22:55
Adding another way for a Tcl-using program
to crash doesn't strike me as a great answer
either. I need more thought on it.

Re-opening and assigning to myself as a
reminder to look again sometime.

Not changing anything checked in though.
It's at least as good as what was there before.
jan.nijtmansplain2017-06-08 14:54:25
Sure, Don, please go ahead! I'm out of ideas for 8.x ....
dgpplain2020-04-16 19:53:06
Fix completed in 8.5 now.
Log in (anonymous is fine) to add a comment.