|
2023-03-22
| ||
| 13:13 | Merge bug-fb368527ae. Bug [fb368527ae] Large string truncation check-in: 077931a7f7 user: pooryorick tags: unchained | |
|
2023-03-04
| ||
| 12:39 | • Ticket [fb368527ae] String truncation in encoding > 2GB length status sti... artifact: 0060b84116 user: jan.nijtmans | |
| 12:39 | • Closed ticket [fb368527ae]. artifact: 0efb982bc6 user: jan.nijtmans | |
|
2023-02-28
| ||
| 10:38 | • Ticket [fb368527ae]: 3 changes artifact: 13abe3d195 user: apnadkarni | |
| 10:28 | Merge bug-fb368527ae. Bug [fb368527ae] Large string truncation check-in: f868c40053 user: apnadkarni tags: trunk, main | |
|
2023-02-27
| ||
| 03:17 | • Ticket [fb368527ae] String truncation in encoding > 2GB length status sti... artifact: 923355a193 user: apnadkarni | |
| 01:57 | • Ticket [fb368527ae]: 3 changes artifact: 17932c90ba user: apnadkarni | |
|
2023-02-26
| ||
| 22:52 | • Ticket [fb368527ae]: 3 changes artifact: 08a21a04a6 user: jan.nijtmans | |
| 17:06 | • Ticket [fb368527ae]: 3 changes artifact: 86c6206a93 user: jan.nijtmans | |
| 17:03 | • Ticket [fb368527ae]: 3 changes artifact: 54d99bcfd9 user: jan.nijtmans | |
| 17:02 | • Ticket [fb368527ae]: 3 changes artifact: a593be7744 user: apnadkarni | |
| 16:51 | • Ticket [fb368527ae]: 3 changes artifact: 7d4686cfab user: apnadkarni | |
| 16:42 | • Ticket [fb368527ae]: 3 changes artifact: e94697b9fd user: jan.nijtmans | |
| 16:39 | • Ticket [fb368527ae]: 3 changes artifact: 5f2b53130c user: apnadkarni | |
| 16:27 | • Ticket [fb368527ae]: 3 changes artifact: f51b917686 user: jan.nijtmans | |
| 16:10 | • Ticket [fb368527ae]: 3 changes artifact: 5855ddf6e9 user: jan.nijtmans | |
| 16:08 | • Ticket [fb368527ae]: 3 changes artifact: 744a9173de user: apnadkarni | |
| 15:51 | • Ticket [fb368527ae]: 3 changes artifact: 5a3e04aa89 user: apnadkarni | |
| 14:32 | • Ticket [fb368527ae]: 3 changes artifact: b84037e0e1 user: jan.nijtmans | |
| 13:01 | • Ticket [fb368527ae]: 4 changes artifact: 935cb59973 user: jan.nijtmans | |
|
2023-02-25
| ||
| 16:33 | • Ticket [fb368527ae]: 3 changes artifact: 023fc2f7a9 user: apnadkarni | |
| 16:29 | Experimental fix for [fb368527ae] - length truncation check-in: eb6626d1e6 user: apnadkarni tags: bug-fb368527ae | |
|
2023-02-24
| ||
| 18:00 | • New ticket [fb368527ae] String truncation in encoding > 2GB length. artifact: 625ed48733 user: apnadkarni | |
String truncation in encoding > 2GB length
As of commit [ba4e192f46]
% string length [set s [string repeat A 4100000000]]
4100000000
% string length [encoding convertto ascii $s]
0
The issue is that Tcl_UtfToExternalDString[Ex] and Tcl_ExternalToUtfDString[Ex] functions take srcLen as Tcl_Size but pass it down to the underlying encoders as parameters of int type resulting in silent truncation or sign flipping of the value. In the above example, the length is treated as treated as negative resulting in an immediate return of 0. If the value was greater than 4G, it would simply return a shortened string.
I am a bit surprised the compiler does not complain about this.
077931a7f7Merge bug-fb368527ae. Bug [fb368527ae] Large string truncation check-in2023-03-22 13:13:13f868c40053Merge bug-fb368527ae. Bug [fb368527ae] Large string truncation check-in2023-02-28 10:28:28eb6626d1e6Experimental fix for [fb368527ae] - length truncation check-in2023-02-25 16:29:09
A proposed fix is in [bug-fb368527ae].
There were two possible ways to fix this
Change the signature of the lower level encoding functions
UtfToUtfProcetc. to takeTcl_Sizeparameters. Or,Change the
Tcl_ExternalToUtfDStringExandTcl_UtfToExternalDStringExfunctions to call the above routines in chunks whose lengths fit into an int.
I chose the second option since changing the lower level encoding involves not only the functions themselves but also the call sites which are many.
Code needs reviewing.
/Ashok
Just one little review remark: [603ae5a10fb14cf9], that's all.
I agree with your solution.
Eventually, Tcl_UtfToExternal() and Tcl_ExternalToUtf() will need to be modified the same way, but I don't think this is triggable by a Tcl script: Those functions are only used in Channel code (which doesn't use size_t) and in Zlib code (where zip-files that big are not supported by Tcl yet. So, this can wait.
Build is failing: [https://github.com/tcltk/tcl/actions/runs/4274069387/jobs/7440407837] It might be that this kind of test simply is not possible on Github Actions, since it doesn't have enough memory available to handle this.
I was afraid of the GitHub actions failing. Any suggestions for the constraint to put on the tests? I think they are important enough to run for every official release at least. I'll await your response to above before merging into trunk. Regarding your review remark, the move of srcLen update was very much intentional, not because of logic (I realize it's not necessary and adds to the diff) but for stylistic reasons. Every loop has some implicit invariants that make it easier to reason about them. One of the invariants for that loop should be that src+srcLen points to end of source data. As such, they should be kept in sync - incrementing one should decrement the other - in as close proximity as possible in the code. Not doing so is potential for future errors. In the original code there was too much needless separation for really marginal gain in efficiency. Still, it's a preference issue so I'll go with your change. /Ashok
One more remark on Tcl_UtfToExternal() and Tcl_ExternalToUtf(). It appears you changed the parameter type dstLen and srcLen on those functions from int to Tcl_Size.
I don't think that is a good idea. Aside from the fact that it is not TIP'ed (I'm not sure I care much about that aspect, it can be corrected), I think functions should clearly deal with either 32-bit lengths or 64 bit lengths and this should be apparent from their signatures (parameter types). Those two functions have input as Tcl_Size and output (via pointers) as ints. This leads to confusion as to operation when inputs exceed 31-bit lengths.
Also, as an aside, although those functions are not widely used within Tcl core, they are public and used by extensions as the Dstring variants do not allow for streaming operations.
I'm also diving into the crash on the read side (which is a thornier issue with multiple places where this size_t->int truncation is happening). Another reason to have a clear demarcation between the 64-bit capable and 32-bit capable functions. If the former need to make use of the latter, they need to call them in a loop with appropriate checks on the parameters (as done in this branch)
I suggest they be kept as 32-bit length functions, at least for now, since the underlying encoders deal with 32-bit lengths. I'll plan on making that change back.
/Ashok
> Any suggestions for the constraint to put on the tests? How about "-constraints perf" ?? It's already used on lpop-99.* testcases, which also are not normally run for performance reasons. I don't mind another constraint name (but then I would change the lpop-99.* constraint name too)
> I suggest they be kept as 32-bit length functions, at least for now, since the underlying encoders deal with 32-bit lengths. I'll plan on making that change back.
Please don't see: https://core.tcl-lang.org/tcl/file?udc=1&ln=446-447&ci=3513e43ea1c3bb5c&name=generic%2FtclZlib.c
What should be done is make Tcl_UtfToExternal()/Tcl_ExternalToUtf() report the error, e.g. by returning TCL_CONVERT_NOSPACE
Thinking about it, it's not even necessary that those functions handle all data available. They just handle the data they can, and report the reason when they stop early.
I'll go with "perf" as the constraint. Although it would be nice to distinguish between tests that take a long time, versus tests that need a lot of resources. There are more important issues to address. /Ashok
> What should be done is make Tcl_UtfToExternal()/Tcl_ExternalToUtf() report the error, e.g. by returning TCL_CONVERT_NOSPACE
See: [d110cdebf8ae9d97]
Jan,
That tclZlib.c example you linked is pretty easy to fix, isn't it? Unless I'm missing something, not enough to convince me I'm afraid!
The use of TCL_CONVERT_SPACE (it was also my first thought) does not help because it does not distinguish between the caller not giving a large enough buffer versus the function Tcl_ExternalToUtf not being able to handle source lengths that large.
Consider being called with srcLen=2GB+1 and dstLen=4GB. The function will return TCL_CONVERT_NOSPACE and the natural thing is for the caller to double it's output buffer! It takes some thinking and additional logic on the part of the programmer (and I guarantee they will not even realize this until they actually hit a bug) to distinguish between the real destination buffer too small case and the source data greater than function limits. Not a good API design imo.
So I still think those functions need to change. The DString variants can continue to take Tcl_Size arguments since there is no confusion there with output parameters. They will need to adapt to loop calling the lower level routines which they do now anyway.
/Ashok
Given both approaches take work on the caller's part, I'll just add that the cleanest solution may be to make everything Tcl_Size-safe throughout (including the lowest level functions). More work now, but may be worth at least a look at feasibility to avoid future pain. What do you think? Is it feasible? /Ashok
> Not a good API design imo.
Oh, I agree on that. The Tcl_UtfToExternal()/Tcl_ExternalToUtf() API is simply too complicated. But keeping srcLen of type 'int' will mean that in the zipfs example the output simply will be truncated (stopping at the first 0-byte) without Tcl_UtfToExternal being able to report the error. Returning TCL_CONVERT_NOSPACE is better than returning TCL_OK (but a separate error-code for this case would even be better)
> What do you think? Is it feasible?
I don't think this is feasable. For 9.1 I would like a complete channel-handling rewrite (using ICU, for example). The Channel code (with reflected channel functionality) is too complex to fix now.
> It appears you changed the parameter type dstLen and srcLen on those functions from int to Tcl_Size.
>
> I don't think that is a good idea. Aside from the fact that it is not TIP'ed ...
Yes, it was TIP'd. See: https://core.tcl-lang.org/tips/doc/trunk/tip/494.md
I'm sorry but those functions are not mentioned in that TIP and a statement like Change the type of the size parameter of many functions which from int (but NOT int *) to size_t. should not be used as the basis for converting arbitrary function parameters unless those functions can successfully deal with the larger data sizes and not just return an error code.
I've still not understood what you are espousing as the benefit of expanding the parameter to Tcl_Size and simply returning an error.
And I've not grokked why you keep referencing the zipfs example. For one thing, it limits output to MAX_COMMENT_LENGTH and MAXPATHLEN in the two cases so int/size_t is hardly an issue there. Second, even if that were not the case, fixing it via a loop calling Tcl_UtToExternal is very straightforward (as is done in the fix proposed for the current bug).
/Ashok
Anyways, I'll think about this some more and may be we can discuss Wed if there is time.
Fixed in trunk in commit [f868c40053]