Menu

#1309 generated tcl code crashes -- easy fix

None
closed-fixed
nobody
tcl (60)
5
2022-02-01
2013-03-26
No

int removeIndex(std::vector<int> indices);</int>

gives code like this:

      {
        Tcl_Obj **listobjv;
        int       nitems;
        int         temp;
        std::vector<int> *v;

        if(SWIG_ConvertPtr(argv[1], (void **) &v, \
            SWIGTYPE_p_std__vectorTint_t, 0) == 0){
          /* wrapped vector */
          _v = 1;
        } else {
          // It isn't a vector<int> so it should be a list of int's
          if(Tcl_ListObjGetElements(interp, argv[1],
              &nitems, &listobjv) == TCL_ERROR)
          _v = 0;
          else
          if (nitems == 0)
          _v = 1;
          //check the first value to see if it is of correct type
          if (Tcl_GetIntFromObj(interp, listobjv[0], &temp) == TCL_ERROR)
          _v = 0;
          else
          _v = 1;
        }

Notice that the nitems == 0 test will not prevent the Tcl_GetIntFromObj to access listobjv[0] which causes a crash.

Discussion

  • Stephen Ehmann

    Stephen Ehmann - 2013-03-26

    I also have this in my .i:
    %template(intVector) std::vector<int>;
    in case that matters</int>

     
  • Olly Betts

    Olly Betts - 2022-01-31
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,6 +1,7 @@
     int removeIndex(std::vector&lt;int&gt; indices);
    
     gives code like this:
    +```
           {
             Tcl_Obj **listobjv;
             int       nitems;
    @@ -25,5 +26,5 @@
               else
               _v = 1;
             }
    -
    +```
     Notice that the nitems == 0 test will not prevent the Tcl_GetIntFromObj to access listobjv[0] which causes a crash.
    
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2022-02-01
    • status: open --> closed-fixed
     
  • Olly Betts

    Olly Betts - 2022-02-01

    I've committed a fix which should be in SWIG 4.2.0:

    commit 6f4adde4b4dccbd31725aeda1c9e17b9178a5550 (HEAD -> master)
    Author: Olly Betts <olly@survex.com>
    Date:   Tue Feb 1 13:08:13 2022 +1300
    
        [Tcl] Fix std_vector typecheck typemaps
    
        In some cases the typecheck typemap would try to access the first
        element of an empty Tcl list.
    
        Fixes https://sourceforge.net/p/swig/bugs/1309/
    
     

Log in to post a comment.

MongoDB Logo MongoDB