Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: ecdbee36b1af0139f9c4f0790ac8f8f776531f96f77477fd6432cb2faf838102
Ticket: 24b918147808e33e9bdb31a516fd29daaddd7149
Fix unsafe buffer lifetime
User & Date: rupprecht on 2021-05-03 18:40:04
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    tl;dr this patch fixes a lifetime bug detected in trunk versions of Clang:
    
    $ git diff generic/tclIO.c
    diff --git a/generic/tclIO.c b/generic/tclIO.c
    index dc028b522..a23fff80f 100644
    --- a/generic/tclIO.c
    +++ b/generic/tclIO.c
    @@ -4296,9 +4296,10 @@ Write(
            nextNewLine = (char *)memchr(src, '\n', srcLen);
         }
     
    +    char safe[BUFFER_PADDING];
         while (srcLen + saved + endEncoding > 0) {
            ChannelBuffer *bufPtr;
    -       char *dst, safe[BUFFER_PADDING];
    +       char *dst;
            int result, srcRead, dstLen, dstWrote, srcLimit = srcLen;
     
            if (nextNewLine) {
    
    Longer summary: a recent update to clang improves optimizations related to memcpy:
    https://github.com/llvm/llvm-project/commit/f5446b769a7929806f72256fccd4826d66502e59.
    
    When we upgraded to a version of clang that includes this optimization, we
    encountered a build error from a step that was consuming the output of a tcl/tk
    script, and found that the tcl script was producing junk output. Upon further
    inspection, this is because the tcl interpreter built by clang was entirely
    optimizing away the "safe" buffer in the patch above. This appears to be valid,
    as the lifetime of the "safe" buffer ends at the closing of the for loop, and
    is not guaranteed to be preserved on the next loop iteration. Moving the buffer
    outside of the for loop will preserve the "safe" buffer for the necessary
    lifetime.
    
  5. foundin changed to: "trunk"
  6. is_private changed to: "0"
  7. login: "rupprecht"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "3ebba1c26822dc664ef602a582d54ffaec6356e8"
  10. resolution changed to: "None"
  11. severity changed to: "Minor"
  12. status changed to: "Open"
  13. submitter changed to: "rupprecht"
  14. subsystem changed to: "53. Configuration and Build Tools"
  15. title changed to: "Fix unsafe buffer lifetime"
  16. type changed to: "Patch"