Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 111d158fa3221418acfc11c49e848c80d77c12db2207b7de2676aad275aad81e
Ticket: ac6792195deb234e0bd7c3dbb9a5849bafa2b8de
avoid signed integer overflow in BinaryEncodeUu()
User & Date: chrstphrchvz on 2021-12-11 06:10:09
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    In <tt>BinaryEncodeUu()</tt> (generic/tclBinary.c), data to encode is bitwise-ORed into <tt>int n</tt> and typically is eventually left-shifted out, causing signed integer overflow which is undefined behavior. [https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan (<tt>-fsanitize=shift-base</tt>)] complaint:
    
    <blockquote><pre>
    $ ./tcltest ../tests/binary.test
    <span style="font-weight:bold">tcl/generic/tclBinary.c:2700:8: <span style="color:red">runtime error:</span> left shift of 1633837921 by 8 places cannot be represented in type 'int'</span>
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclBinary.c:2700:8 in 
    <span style="font-weight:bold">tcl/generic/tclBinary.c:2707:8: <span style="color:red">runtime error:</span> left shift of 1667326563 by 8 places cannot be represented in type 'int'</span>
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclBinary.c:2707:8 in 
    binary.test:	Total	717	Passed	640	Skipped	77	Failed	0
    Number of tests skipped for each constraint:
    	76	bigEndian
    	1	win
    </pre></blockquote>
    
    This can be avoided by changing <tt>n</tt> to an <tt>unsigned int</tt>:
    
    <pre>
    diff --git generic/tclBinary.c generic/tclBinary.c
    index 0f8f77e45..6bbc8fc9f 100644
    --- generic/tclBinary.c
    +++ generic/tclBinary.c
    @@ -2599,7 +2599,8 @@ BinaryEncodeUu(
     {
         Tcl_Obj *resultObj;
         unsigned char *data, *start, *cursor;
    <span style="color:red">-    int offset, count, rawLength, n, i, j, bits, index;</span>
    <span style="color:green">+    int offset, count, rawLength, i, j, bits, index;</span>
    <span style="color:green">+    unsigned int n;</span>
         int lineLength = 61;
         const unsigned char SingleNewline[] = { UCHAR('\n') };
         const unsigned char *wrapchar = SingleNewline;
    </pre>
    
  5. foundin changed to: "8.6.12"
  6. is_private changed to: "0"
  7. login: "chrstphrchvz"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "chrstphrchvz"
  13. subsystem changed to: "16. Commands A-H"
  14. title changed to: "avoid signed integer overflow in BinaryEncodeUu()"
  15. type changed to: "Patch"