Tcl Source Code

View Ticket
Login
Ticket UUID: 2738427
Title: Tcl_NumUtfChars(...) no overflow check
Type: Bug Created on: 2009-04-06 19:41:37
Submitter: dgp Assigned to: dgp
Subsystem: 44. UTF-8 Strings Severity: Minor
Priority: 5 Medium Last modified: 2020-04-16 19:53:06
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2020-04-16 19:53:06
Version: None
Description:
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.
User Comments:
dgp added on 2009-04-11 03:09:53:
comment test....

jan.nijtmans added on 2017-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.nijtmans added on 2017-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.


dgp added on 2017-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.nijtmans added on 2017-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!


dgp added on 2017-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.nijtmans added on 2017-06-08 14:54:25:
Sure, Don, please go ahead! I'm out of ideas for 8.x ....

dgp added on 2020-04-16 19:53:06:
Fix completed in 8.5 now.