|
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
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.
Fix for this: [b3969d0fab196360]
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.
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