Tcl Source Code

View Ticket
Login
Ticket UUID: 3e0a2d99f365c035ce84ec9de1e3acd80c2e2c6c
Title: [string first] failures on 32-bit platforms
Type: Bug Created on: 2018-12-12 14:06:27
Submitter: dgp Assigned to: jan.nijtmans
Subsystem: 10. Objects Severity: Important
Priority: 5 Medium Last modified: 2019-01-16 20:16:41
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2019-01-16 20:16:41
Version: 9.0a0
Description:
First reported by "de" on the chat: http://paste.tclers.tk/4877

==== string-4.12.0 string first, start index FAILED
==== Contents of test case:

    run {string first \u7266 abc\u7266x 4}

---- Result was:
4294967295
---- Result should have been (exact matching):
-1
==== string-4.12.0 FAILED



==== string-4.8.1 string first FAILED
==== Contents of test case:

    run {string first "" x123xx345xxx789xxx012}

---- Result was:
4294967295
---- Result should have been (exact matching):
-1
==== string-4.8.1 FAILED



==== string-4.12.1 string first, start index FAILED
==== Contents of test case:

    run {string first \u7266 abc\u7266x 4}

---- Result was:
4294967295
---- Result should have been (exact matching):
-1
==== string-4.12.1 FAILED

First fails with http://core.tcl.tk/tcl/info/1f55c24720

But that appears to be new testing exposing an existing problem.

TclStringFirst() on trunk returns size_t, but also needs to return
the literal value -1 to signal 'not found'. That's not cleanly consistent.

The caller then takes the result as input to Tcl_NewWideIntObj().
On 32-bit systems, wide is larger than size_t and we get conversion
to the wrong value.
User Comments:
dgp added on 2018-12-12 14:10:20:
"de" goes on to credit "emiliano"

I'm just happy someone is testing 32-bit systems, and I hope it would happen more often.

sebres added on 2018-12-12 19:25:12:

First shot in [d7843f5d69] closes the issue, but introduces another problem: `string first` can't return a value larger that INT_MAX (on x64 platform) anymore, and trunk is pointed in that direction.

There is another fix [080ecbe60a] as branch fix-3e0a2d99f3-str-first-x86-x64 solves it for both platform.

BTW. As discussed on chat there are several another issues on 32-bit platform regarding the indices as well as TIP#502:

- TclGetIntForIndexM seem to have a bug for cases like
`string first a aaa 4294967295` because it results in 0 and not -1 on x86.

- looks like unexpected result (esp. after TIP 502 got merged):
`string last a aaa -1` or `string last a aaa end-5` produce 2
I meant as opposite to `string first a aaa 5` and `string first a aaa end+1` (where both results to -1).


jan.nijtmans added on 2018-12-12 20:29:03:
Thanks for noticing and fixing this!  Merged to trunk now, with little change: The new function moved to tclInt.h

jan.nijtmans added on 2018-12-12 20:45:50:
I'll have a look at the remaining examples too. Thanks!

sebres added on 2018-12-13 07:48:44:

I had to fix the "little change" partially back again (otherwise this had caused a dual invocation of TclStringFirst/TclStringLast), see [e1b31151e5].
In order to prevent this (or even some UB's), please always try to avoid the usage of macro's as functions, if inside it the parameters are used multiple times.


jan.nijtmans added on 2018-12-13 08:56:50:
> (otherwise this had caused a dual invocation of TclStringFirst/TclStringLast)

I don't think my macro causes a dual invocation at all ;-)   Please have another look.

The trick is to do a +1, then cast to (Tcl_WideInt), followed by a -1.  That's more efficient than an if-statement.

sebres added on 2018-12-13 09:44:25:
Hmm, right... Just as I've noticed some compiler warning (I think it was gcc 7.x or 8.0, -m32 -O3) I had take a look into diff here in fossil-ui and saw the macro, but overlooked the change with you smarter +1-1 casting part, as it was hidden (because of scrollbars).
So sorry for the wrong reprovals from my side ;)

Although I think the compiler will optimize it, anyway you are right, thus reverted back to your revision, with a little change from me, (you've forgot a cast to (size_t)(value) by increment in macro, if somewhere later another type as size_t will be used as value).

Thanks.

jan.nijtmans added on 2018-12-13 21:17:30:

Should be fixed now in core-8-branch and trunk.

Still TODO: Handle indexes > 2Gb, but that's not what this bug is about


sebres added on 2018-12-14 13:19:16:

BTW, we don't need to handle indexes with offset > 2Gb, because allocating of single block with this size is impossible in 32-bit process. Let alone there is no 32-bits platforms (at least x86/ARM), that allow you to have common process memory > 2Gb at all. Just try:

% string repeat " " 0x7fffffff
string size overflow, out of memory allocating 2147483648 bytes


sebres added on 2019-01-15 12:43:22:

Found new tests failed for similar reasons on 32-bit systems (even on border case unsigned (size_t)-1 to signed wide-int conversion):

lset (16.5, 16.6, 16.7) - failed with out of range instead of set element. string (6.102.0/1, 31.9a.0/1) - failed with 4294967295 instead of -1.

Fixed now in [175e053939] and [8a67f59280].

Additionally [8a67f59280] introduces now new internal macro TclWideIntFromSize for correct conversion of size_t to wide-int to consider platform-related "negative" value ((size_t)-1), if wide-int and size_t have different dimensions (e. g. on 32-bit platforms).


jan.nijtmans added on 2019-01-16 20:16:41:
Thank you, sebres, for those fixes! It's appreciated!