Artifacts Associated With Ticket fb368527aeed240f
Ticket change [625ed48733] (rid 209150) by apnadkarni on 2023-02-24 18:00:21:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-markdown"
- comment initialized to:
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.
- foundin initialized to: "9.0"
- is_private initialized to: "0"
- login: "apnadkarni"
- priority initialized to: "5 Medium"
- private_contact initialized to: "9546e9877f1a8899e25a89082561ee694e7fb402"
- resolution initialized to: "None"
- severity initialized to: "Important"
- status initialized to: "Open"
- submitter initialized to: "apnadkarni"
- subsystem initialized to: "16. Commands A-H"
- title initialized to: "String truncation in encoding > 2GB length"
- type initialized to: "Bug"
Ticket change [023fc2f7a9] (rid 209210) by apnadkarni on 2023-02-25 16:33:28:
- icomment:
A proposed fix is in [bug-fb368527ae]. There were two possible ways to fix this - Change the signature of the lower level encoding functions `UtfToUtfProc` etc. to take `Tcl_Size` parameters. Or, - Change the `Tcl_ExternalToUtfDStringEx` and `Tcl_UtfToExternalDStringEx` functions 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
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [935cb59973] (rid 209235) by jan.nijtmans on 2023-02-26 13:01:35:
- assignee changed to: "apnadkarni"
- icomment:
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.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
Ticket change [b84037e0e1] (rid 209243) by jan.nijtmans on 2023-02-26 14:32:14:
- icomment:
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.
- login: "jan.nijtmans"
- mimetype: "text/plain"
- icomment:
Ticket change [5a3e04aa89] (rid 209246) by apnadkarni on 2023-02-26 15:51:39:
- icomment:
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
- login: "apnadkarni"
- mimetype: "text/plain"
- icomment:
Ticket change [744a9173de] (rid 209251) by apnadkarni on 2023-02-26 16:08:04:
- icomment:
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
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [5855ddf6e9] (rid 209252) by jan.nijtmans on 2023-02-26 16:10:38:
- icomment:
> 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)
- login: "jan.nijtmans"
- mimetype: "text/plain"
- icomment:
Ticket change [f51b917686] (rid 209253) by jan.nijtmans on 2023-02-26 16:27:45:
- icomment:
> 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.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [5f2b53130c] (rid 209254) by apnadkarni on 2023-02-26 16:39:38:
- icomment:
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
- login: "apnadkarni"
- mimetype: "text/plain"
- icomment:
Ticket change [e94697b9fd] (rid 209258) by jan.nijtmans on 2023-02-26 16:42:19:
- icomment:
> What should be done is make Tcl_UtfToExternal()/Tcl_ExternalToUtf() report the error, e.g. by returning TCL_CONVERT_NOSPACE See: [d110cdebf8ae9d97]
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [7d4686cfab] (rid 209259) by apnadkarni on 2023-02-26 16:51:09:
- icomment:
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
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [a593be7744] (rid 209262) by apnadkarni on 2023-02-26 17:02:31:
- icomment:
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
- login: "apnadkarni"
- mimetype: "text/plain"
- icomment:
Ticket change [54d99bcfd9] (rid 209263) by jan.nijtmans on 2023-02-26 17:03:43:
- icomment:
> 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)
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [86c6206a93] (rid 209264) by jan.nijtmans on 2023-02-26 17:06:51:
- icomment:
> 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.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [08a21a04a6] (rid 209280) by jan.nijtmans on 2023-02-26 22:52:27:
- icomment:
> 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]
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [17932c90ba] (rid 209281) by apnadkarni on 2023-02-27 01:57:52:
- icomment:
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
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [923355a193] (rid 209285) by apnadkarni on 2023-02-27 03:17:04:
- icomment:
Anyways, I'll think about this some more and may be we can discuss Wed if there is time.
- login: "apnadkarni"
- mimetype: "text/plain"
- icomment:
Ticket change [13abe3d195] (rid 209326) by apnadkarni on 2023-02-28 10:38:09:
- icomment: "Fixed in trunk in commit [f868c40053]"
- login: "apnadkarni"
- mimetype: "text/x-fossil-plain"
Ticket change [0efb982bc6] (rid 209550) by jan.nijtmans on 2023-03-04 12:39:02:
- closedate changed to: "2460008.02710852"
- closer changed to: "jan.nijtmans"
- login: "jan.nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"
Ticket change [0060b84116] (rid 209551) by jan.nijtmans on 2023-03-04 12:39:03:
- closedate changed to: "2460008.02712712"
- login: "jan.nijtmans"
- mimetype: "text/plain"