Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: ee890baf5a38b101f9e0ffd59fe96b21d5138ed2280e43ea52ae901e7f762ae8
Ticket: de243b694daaf06e6a0fbcbeb7279c326c25551c
Several trim mechanisms cause segfault / wrong check for NTS
User & Date: sebres on 2018-03-13 19:03:08
Changes

  1. comment changed to:
    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):
    <code><pre>
        if ((bytes[numBytes] != '\0') || (trim[numTrim] != '\0')) {
    	Tcl_Panic("TclTrimRight works only on null-terminated strings");
        }
    </pre></code>
    
    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.
    
  2. login: "sebres"
  3. mimetype: "text/plain"