Skip to content
Tcl Source Code History Of Ticket 6ef312dcf5
Login

History Of Ticket 6ef312dcf5

Artifacts Associated With Ticket 6ef312dcf5

  1. Ticket change [b21da8d5a8] (rid 196822) by chrstphrchvz on 2021-12-16 04:31:11:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      If <tt>Tcl_CreateMathFunc()</tt> (generic/tclBasic.c) is called with 0 and <tt>NULL</tt> for <tt>numArgs</tt> and <tt>argTypes</tt> respectively, as done in <tt>Tcltest_Init()</tt> (generic/tclTest.c), this causes <tt>memcpy()</tt> to be called with <tt>NULL</tt> as the source, which is undefined behavior even though the number of bytes to copy is 0. [https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan (<tt>-fsanitize=nonnull-attribute</tt>)] complaint when used with GCC (as not all common standard library implementations declare <tt>memcpy()</tt> with source or destination pointers as non-<tt>NULL</tt>):
      
      <pre>
      $ ./tcltest
      <span style="font-weight: bold">tcl/generic/tclBasic.c:3557:2: <span style="color:red">runtime error:</span> null pointer passed as argument 2, which is declared to never be null</span>
      %
      </pre>
      
      To avoid this, I would only call <tt>memcpy()</tt> (and <tt>ckalloc()</tt>) when <tt>numArgs</tt> is positive:
      
      <pre>
      diff --git generic/tclBasic.c generic/tclBasic.c
      index fb8524180..45a25ca64 100644
      --- generic/tclBasic.c
      +++ generic/tclBasic.c
      @@ -3550,8 +3550,12 @@ Tcl_CreateMathFunc(
      <span> </span>
           data->proc = proc;
           data->numArgs = numArgs;
      <span style="color:red">-    data->argTypes = (Tcl_ValueType *)ckalloc(numArgs * sizeof(Tcl_ValueType));
      -    memcpy(data->argTypes, argTypes, numArgs * sizeof(Tcl_ValueType));</span>
      <span style="color:green">+    data->argTypes = NULL;
      +    if (0 < numArgs) {
      +	data->argTypes =
      +		(Tcl_ValueType *)ckalloc(numArgs * sizeof(Tcl_ValueType));
      +	memcpy(data->argTypes, argTypes, numArgs * sizeof(Tcl_ValueType));
      +    }</span>
           data->clientData = clientData;
      <span> </span>
           Tcl_DStringInit(&bigName);
      </pre>
      
    5. foundin initialized to: "8.6.12"
    6. is_private initialized to: "0"
    7. login: "chrstphrchvz"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "chrstphrchvz"
    13. subsystem initialized to: "52. Portability Support"
    14. title initialized to: "memcpy() with NULL source in Tcl_CreateMathFunc()"
    15. type initialized to: "Patch"
  2. Ticket change [58f6c24ae6] (rid 196846) by jan.nijtmans on 2021-12-16 14:16:24:

    1. icomment:
      Fixed [c8b818c8cd|here]. Didn't bother about the ckalloc(), since Tcl_CreateMathFunc() is deprecated (it's totally removed in Tcl 9.0). Setting data->argTypes to NULL where it was not NULL before, I'm not sure what other possible ill-effects that could mean for extensions using Tcl_CreateMathFunc(). This isn't called by Tcl core any more, so testcases don't tell the whole story here.
      
      Thanks for the report and the patch!
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  3. Ticket change [61992654e9] (rid 196847) by jan.nijtmans on 2021-12-16 14:17:10:

    1. assignee changed to: "jan.nijtmans"
    2. closedate changed to: "2459565.09525907"
    3. closer changed to: "jan.nijtmans"
    4. login: "jan.nijtmans"
    5. mimetype: "text/plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"