Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2019-01-16
20:16 • Ticket [3e0a2d99f3] string first failures on 32-bit platforms status still... artifact: e3992be268 user: jan.nijtmans
2019-01-15
12:43 • Ticket [3e0a2d99f3]: 4 changes artifact: c6998a6de8 user: sebres
12:34
next amend to [3e0a2d99f3]: fixes TclGetIntForIndex for border case of end offset (size_t)-1 if empt... check-in: 8a67f59280 user: sebres tags: trunk
11:38
fixes out-of-range index [string is ... -fail idx ...] on 32-bit platform (should return -1 instead ... check-in: 175e053939 user: sebres tags: trunk
2018-12-14
13:19 • Ticket [3e0a2d99f3] string first failures on 32-bit platforms status still... artifact: ab4d2eb6b0 user: sebres
2018-12-13
21:17 • Closed ticket [3e0a2d99f3]. artifact: 9944fa891c user: jan.nijtmans
09:44 • Ticket [3e0a2d99f3]: 3 changes artifact: b58a70251d user: sebres
08:56 • Ticket [3e0a2d99f3]: 3 changes artifact: 135d0758cf user: jan.nijtmans
07:48 • Ticket [3e0a2d99f3]: 3 changes artifact: 7a81b8b23b user: sebres
2018-12-12
20:45 • Open ticket [3e0a2d99f3]. artifact: dead274157 user: jan.nijtmans
20:29 • Closed ticket [3e0a2d99f3]. artifact: 22623ea8e5 user: jan.nijtmans
20:28
Fix [3e0a2d99f365c035ce84ec9de1e3acd80c2e2c6c|3e0a2d99f3], for "string last" as well. check-in: f7f4130007 user: jan.nijtmans tags: trunk
19:25 • Ticket [3e0a2d99f3] string first failures on 32-bit platforms status still... artifact: 4ee43d5d5c user: sebres
17:00
better fix for [3e0a2d99f3], considering [string first] could return a value larger that INT_MAX for... closed check-in: 080ecbe60a user: sebres tags: fix-3e0a2d99f3-str-first-x86-x64
14:59
fixes [3e0a2d99f3]: size_t is smaller as wideInt on 32-bit platform (so by not found, -1 results int... check-in: d7843f5d69 user: sebres tags: trunk
14:10 • Ticket [3e0a2d99f3] string first failures on 32-bit platforms status still... artifact: f713223d6e user: dgp
14:06 • Ticket [3e0a2d99f3]: 3 changes artifact: 507a281b19 user: dgp
14:06 • New ticket [3e0a2d99f3]. artifact: 2c264fd455 user: dgp

[string first] failures on 32-bit platforms

Closed / Fixed3e0a2d99f365c035ce84ec9de1e3acd80c2e2c6cCreated 8 years agoUpdated 8 years ago
Descriptionplain2018-12-12 14:06:27
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.
Referenced by5
  • 8a67f59280next amend to [3e0a2d99f3]: fixes TclGetIntForIndex for border case of end offset (size_t)-1 if empty subject, (e. g. failed with out of range by `lset lst end+1 ...` on empty list) on 32-bit system; introduces 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). check-in2019-01-15 12:34:27
  • 175e053939fixes out-of-range index [string is ... -fail idx ...] on 32-bit platform (should return -1 instead of 4294967295), almost the same case as [3e0a2d99f3] check-in2019-01-15 11:38:22
  • f7f4130007Fix [3e0a2d99f365c035ce84ec9de1e3acd80c2e2c6c|3e0a2d99f3], for "string last" as well. check-in2018-12-12 20:28:13
  • 080ecbe60abetter fix for [3e0a2d99f3], considering [string first] could return a value larger that INT_MAX for x64 check-in2018-12-12 17:00:59
  • d7843f5d69fixes [3e0a2d99f3]: size_t is smaller as wideInt on 32-bit platform (so by not found, -1 results into 4294967295) check-in2018-12-12 14:59:36
dgp Ticket [3e0a2d99f3]: 3 changes [History diff]2018-12-12 14:06:47
dgpplain2018-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.
sebreswiki2018-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.nijtmansplain2018-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.nijtmansplain2018-12-12 20:45:50
I'll have a look at the remaining examples too. Thanks!
sebreswiki2018-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.nijtmansplain2018-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.
sebresplain2018-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.nijtmanswiki2018-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

sebreswiki2018-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

sebreswiki2019-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.nijtmansplain2019-01-16 20:16:41
Thank you, sebres, for those fixes! It's appreciated!
Log in (anonymous is fine) to add a comment.