Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2018-03-14
17:39 • Ticket [de243b694d] Several trim mechanisms cause segfault / wrong check for NTS... artifact: 6d2272c22e user: sebres
01:32 • Closed ticket [de243b694d]. artifact: 16eba34337 user: dgp
2018-03-13
21:45 • Ticket [de243b694d]: 3 changes artifact: 58ae45b79a user: dgp
19:03 • Ticket [de243b694d]: 3 changes artifact: ee890baf5a user: sebres
18:59 • Ticket [de243b694d]: 4 changes artifact: 3b8e06c0e7 user: sebres
18:57 • New ticket [de243b694d]. artifact: 3ea7a42c2f user: sebres

Several trim mechanisms cause segfault / wrong check for NTS

Closed / Fixedde243b694daaf06e6a0fbcbeb7279c326c25551cCreated 9 years agoUpdated 9 years ago
Descriptionwiki2018-03-13 18:57:57
I noticed segfaults in case of TclTrim (left/right) for non-NTS string representation (I know, this is almost unexpected, but may happen using some modules).

Very strange is that it may cause segfault even by validity check of the NTS-string (to avoid segfault)! which is IMHO do not proper implemented: Even to protect a non-usage of such non-NTS string, Tcl checks currently using access of byte, that may be out of range (so causes access violation instead of panic):

    if ((bytes[numBytes] != '\0') || (trim[numTrim] != '\0')) {
	Tcl_Panic("TclTrimRight works only on null-terminated strings");
    }

I repeat again, I know that non-NTS is not "good" at all, but if nevertheless occurred, this one - `bytes[numBytes]` is unexpected as proper validity check for NTS (even because instead of panic you can get segfault).

Thus rewritten now using more safe variant (implemented without panic, using new function TclUtfWellFormedEnd, that can do the same check, but without access of bytes outside of bounds).

A branch [sebres-8-5-fix-trim-on-mailformed] fixes this.

This checks the end-character is well-formed utf8-sequence now (and if not, does in-place call of trim on proper string representation, which contains a NTS zero-byte at end).

Additionally:

- both work well now on non-NTS strings (without null character at end) without panic;
- fixed wrong offsets (trim-length if last char was malformed in case of out of range), for example if this character (like `\xC0\x00` will recognized as 2 bytes char and trimmed with too long offset, results previously in negative length);
- new function TclTrim introduced (as safe combination of TclTrimLeft/TclTrimRight).

Please review and merge in 8.6 and trunk.

sebreswiki2018-03-13 18:59:19

Fix for this: [b3969d0fab196360]

sebres Ticket [de243b694d]: 3 changes [History diff]2018-03-13 19:03:08
dgpplain2018-03-13 21:45:19
I was afraid this was bringing in a lot of copying, but I see that
it is just a safety check, and the copying is done only when
needed to establish the pre-conditions of the existing TclTrim*
routines.  That's nicer than a panic, though it does call into
question what the caller is doing wrong.

The TclTrim* routines have been internal only and all the
internal callers meet the pre-conditions (If not, *that's* the
bug to fix!), so this is a tiny bit of waste not strictly needed,
but it makes  things more robust and potentially useful to
more callers without a lot of expense.  Seems OK.

Let me commit a few tweaks, and then I can merge.
dgp Closed ticket [de243b694d]. [History diff]2018-03-14 01:32:43
sebreswiki2018-03-14 17:39:39

Thanks Don, good job.

I've improved it a bit now (for 8.5 and 8.6).

The single char "special fix" [96a6332cff] simply avoids double overhead in such simple cases (where internal trim-left results in one char, which is already checked there):

  % timerate {string trimleft a " "}
  0.024563 µs/# 13629326 # 40711412 #/sec 334.779 nett-ms
  % timerate {string trim a " "}
- 0.038242 µs/# 11469338 # 26149583 #/sec 438.605 nett-ms
+ 0.026118 µs/# 13351457 # 38287919 #/sec 348.712 nett-ms

Log in (anonymous is fine) to add a comment.