Skip to content
Tcl Source Code History Of Ticket 3598580fffffffff
Login

History Of Ticket 3598580fffffffff

Artifacts Associated With Ticket 3598580fffffffff

  1. Ticket change [aa2694e81e] (rid 99273) by apnadkarni on 2012-12-27 02:42:51:

    1. assignee initialized to: "nijtmans"
    2. cmimetype initialized to: "text/plain"
    3. comment initialized to:
      in Tcl_ListObjReplace, the deleted elements are Tcl_DecrRefCount'ed BEFORE new elements are Tcl_IncrRefCount'ed. Isn't that a problem if the inserted element happens to be in the list (and only in the list). It would be freed, and the freed memory would be added back to the list. It wouold have to be from custom code  (for example, moves an element to the end). Not from script or Tcl_Eval where the objects are ref'ed on the stack and hence will not be released after the Tcl_DecrRefCount.
      
      
      
    4. foundin initialized to: "None"
    5. is_private initialized to: "0"
    6. priority initialized to: "5 Medium"
    7. resolution initialized to: "None"
    8. status initialized to: "Open"
    9. submitter initialized to: "apnadkarni"
    10. subsystem initialized to: "14. List Object"
    11. title initialized to:
      Tcl_ListObjReplace may release deleted elements too early
      
    12. type initialized to: "Bug"
  2. Ticket change [f6a2eb5112] (rid 130329) by apnadkarni on 2012-12-27 09:43:14:

    1. foundin changed to: "current: 8.6.0"
  3. Ticket change [93fa5afd75] (rid 105902) by nijtmans on 2012-12-27 21:16:01:

    1. foundin changed to: "obsolete: 8.5.13"
    2. icomment:
      Agreed. Same in 8.4 and 8.5 as well
      
      
    3. login: "nijtmans"
    4. mimetype: "text/plain"
    5. resolution changed to: "Accepted"
    6. username: "nijtmans"
  4. Ticket change [c201a4b29e] (rid 111424) by nijtmans on 2012-12-27 22:08:52:

    1. icomment:
      Fixed in all branches
      
      
    2. login: "nijtmans"
    3. mimetype: "text/plain"
    4. username: "nijtmans"
  5. Ticket change [65a7c365fd] (rid 102589) by nijtmans on 2012-12-27 22:08:53:

    1. closedate changed to: "2012-12-27T15:08:53"
    2. closer changed to: "nijtmans"
    3. icomment:
      
      
      allow_comments - 1
      
      
      
    4. login: "nijtmans"
    5. mimetype: "text/plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"
    8. username: "nijtmans"
  6. Ticket change [ffc5cd3df6] (rid 108818) by ferrieux on 2012-12-27 23:57:17:

    1. icomment:
      
      
      allow_comments - 0
      
      Jan, two nitpicking questions ;)
      
      (1) there's an early return path (AttemptNewList); in that case the incRef'ed objv[*] are leaked. Admittedly that happens only for very large lists / lowmem conditions, but ...
      
      (2)  at the point of the former late-incrRef code, there was previously a comment explaining "no memcopy because we need to iterate anyway". Since we no longer do, what about using memcopy (or memmove) ?
      
      
      
      
    2. login: "ferrieux"
    3. mimetype: "text/plain"
    4. username: "ferrieux"
  7. Ticket change [dd9e81a2df] (rid 114706) by nijtmans on 2012-12-28 00:20:07:

    1. icomment:
      >Jan, two nitpicking questions ;)
      Please do! Your 'nitpicking' is always appreciated!
      >there's an early return path ....
      There already was a (possible) memory leak in this case, if
      the objv array contains values with refcount 0. Strictly speaking,
      if incrementing refcounts first, then in case of TCL_ERROR
      they should be decremented again before returning. But
      doing this in 8.x is a behaviour change: it might free objects
      that wouldn't be freed before. Since it's so rare, I would
      prefer to keep it as is, at least in 8.0. For 9.0, better
      fix it, that would prevent a leak in all situations.
      > what about using memcopy
      Good idea. Not worth to do it for 8.x, but for 9.0
      this code could definetely have an overhoul.
      
      
    2. login: "nijtmans"
    3. mimetype: "text/plain"
    4. username: "nijtmans"
  8. Ticket change [508422a60f] (rid 125986) by dgp on 2012-12-28 00:51:48:

    1. icomment:
      Where's the demo script?  The tests?
      
      
    2. login: "dgp"
    3. mimetype: "text/plain"
    4. username: "dgp"
  9. Ticket change [36140839cd] (rid 107058) by ferrieux on 2012-12-28 03:18:40:

    1. icomment:
      > [Don] Where's the demo script? The tests?
      
      Unfortunately, as Ashok points out, the script level alone can't trigger this (extra refs in the stack frame).
      
      
      
      
    2. login: "ferrieux"
    3. mimetype: "text/plain"
    4. username: "ferrieux"
  10. Ticket change [18bc685234] (rid 122709) by ferrieux on 2012-12-28 03:28:23:

    1. icomment:
      > [Jan] it might free objects that wouldn't be freed before
      
      Oh, you're right ! Strike that suggestion :}
      
      This is yet another reason for my distrust of the choice "create objects at refcount zero".
      So far I had only aesthetic concerns about the two distinct delete-paths (1->0 and 0->-1), but this is a really concrete case where it does matter: thanks !
      
      
    2. login: "ferrieux"
    3. mimetype: "text/plain"
    4. username: "ferrieux"
  11. Ticket change [9fbacfc5e6] (rid 96691) by ferrieux on 2012-12-28 03:33:30:

    1. icomment:
      Uh, what about a TclDecrRefCountNoDeleteAtZero ? (ie one that frees only at -1)
      (could stay internal... or not, given Ashok's goals ;)
      
      
    2. login: "ferrieux"
    3. mimetype: "text/plain"
    4. username: "ferrieux"
  12. Ticket change [c86f22a9b3] (rid 122102) by nijtmans on 2012-12-28 03:58:22:

    1. closedate changed to: "2012-12-27T20:58:22"
    2. closer changed to: "nijtmans"
    3. icomment:
      >Uh, what about a TclDecrRefCountNoDeleteAtZero ?
      Are you serious? Please don't go that way.
      >....my distrust of the choice "create objects at
      > refcount zero"....
      This choice served Tcl well, but it must be used
      with care. transfering an object with refCount=0
      means: please remove it when you are done with
      it. transfering an object with refCount=1 means
      that the caller expects it to exist when the function
      call returns. Unfortunately, Tcl_ListObjReplace
      violates that rule in the TCL_ERROR case, which
      causes a possible memory leak. 
      
      For 8.4, the escape path doesn't exist, so
      everything is OK.
      For 8.[56], the refcounts could be restored by:
      <http://core.tcl.tk/tcl/info/d5147fc677>
      For 9.x, a Tcl_DecrRefCount would be
      better here, noting a POTENTIAL INCOMPATIBILITY.
      
      Alex, (and Don) how do you think about that?
      
      
    4. login: "nijtmans"
    5. mimetype: "text/plain"
    6. status changed to: "Pending"
    7. username: "nijtmans"
  13. Ticket change [728537cd4c] (rid 100872) by apnadkarni on 2012-12-28 11:18:13:

    1. icomment:
      In response to dgp's request, here is a critcl code fragment to illustrate the problem. As said earlier, because  of ref counts, not clear how to demo from a script level.
      
      critcl::ccommand crash {} {
          Tcl_Obj *objP;
          if (objc != 2) {
      Tcl_WrongNumArgs(interp, 1, objv, "LIST");
      return TCL_ERROR;
          }
      
          /* Just demo - No error checking */
      
          if (Tcl_IsShared(objv[1])) {
              Tcl_SetResult(interp, "Need unshared list obj for test", TCL_STATIC);
              return TCL_ERROR;
          }
      
          /* Nonsensical but legal */
          Tcl_ListObjIndex(interp, objv[1], 1, &objP);
          Tcl_ListObjReplace(interp, objv[1], 1, 2, 1, &objP);
          Tcl_SetObjResult(interp, objv[1]);
          return TCL_OK;
      }
      
      The following will then result in a panic on windows: 
      C:\src\tarray\src>tclsh86t
      % proc history args {}
      % crash
      invalid command name "crash"
      % source tarray.critcl
      % crash
      wrong # args: should be "crash LIST"
      % set v [list ghi fgr tke]
      ghi fgr tke
      % crash $v[set v ""]
      ghi fgr
      % set x "afasdf"
      <Panic>
      Tracing verifies that the second element is freed via TclpFree, put on the cache and then added back to the list, then assigned to x
      
      
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
    4. status changed to: "Open"
    5. username: "apnadkarni"
  14. Ticket change [f0bcab8149] (rid 109245) by apnadkarni on 2012-12-28 13:01:27:

    1. icomment:
      Verified crash does not happen with Jan's trunk patch
      
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
    4. username: "apnadkarni"
  15. Ticket change [c1a0d1017b] (rid 111350) by dgp on 2012-12-29 12:57:33:

    1. icomment:
      Thank you for the demo to clarify matters.
      
      In the demo, the caller of T_LOR appears to
      be buggy in its management of refcounts.
      
      In the demo, the value "fgr" comes into the
      command procedure of [crash] with a refcount
      of 1, accounting for the reference that's stored in
      the list.
      
      After the call to Tcl_ListObjIndex() another reference
      to that value is stored in objP.  To account for the
      second reference, at that point there should be a
       
          Tcl_IncrRefCount(objP);
      
      With that correct refcount management in place, there
      should be no issue left with the Tcl_ListObjReplace()
      that we've all been using a long time now.
      
      A balancing Tcl_DecrRefCount(objP) before returning
      from the [crash] command procedure would then account
      for that variable going out of scope.
      
      It's true that a buggy C caller can crash the program.  That's
      been part of the power and responsibility of C callers as long
      as I've used Tcl.  The Tcl C library fairly explicit does not attempt
      to armor plate itself against buggy C callers.
      
      Unless there's some other non-buggy demo of this issue, I'd
      say there's not a bug here in the sense of something that demands
      to be fixed.  If that's correct, then I'd go on to say we ought not
      be messing with the 8.4 and 8.5 implementations over this.
      
      The improvement in additional safety against buggy C callers is
      something I'd consider in 8.6 patchlevels, but even there only
      when weighed against whatever negative impact might come
      along with the patch.  Does it significantly impact performance 
      in any reasonable scenarios?
      
      
    2. login: "dgp"
    3. mimetype: "text/plain"
    4. username: "dgp"
  16. Ticket change [a4f5d36e7f] (rid 110290) by nijtmans on 2012-12-29 16:29:08:

    1. icomment:
      Let me respectfully disagree with your diagnostics that
      there is no bug here. The Tcl_ListObjReplace is expected
      to be fire-and-forget: when the objP object is not
      Tcl_IncRefCount'ed means that after the
      Tcl_ListObjReplace call this object is no longer
      used by the caller. In this case, that's true,
      so the caller should not need to increment the
      refCount. Yes, the example code is
      meaningless, but valid. From the documentation
      cannot be derived that the refcount should
      be incremented before the Tcl_ListObjReplace
      call, and decremented immediately after.
      
      The only thing my patch does is do the
      increment before the decrement, as suggested
      by Ashok, nothing else changed. I now corrected
      the escape path as well, restoring the
      refcounts as they were.
      
      Extreme care should be taken doing
      this in any 8.x releases, but with 4 pair of
      eyes focussed on this bug and all
      existing test-cases as back-up, I think
      this change should be fine.
      
      I am trying to turn this into a test-case
      for the test suite, but that's not
      finished yet.
      
      
    2. login: "nijtmans"
    3. mimetype: "text/plain"
    4. username: "nijtmans"
  17. Ticket change [73d73fccf6] (rid 105100) by nijtmans on 2012-12-29 16:33:15:

    1. icomment:
      > but even there only
      > when weighed against whatever negative impact might come
      > along with the patch. Does it significantly impact performance
      > in any reasonable scenarios?
      Changing the order of calls doesn't influence the performance,
      I don't see any possible negative impact here.
      
      
    2. login: "nijtmans"
    3. mimetype: "text/plain"
    4. username: "nijtmans"
  18. Ticket change [1a656b8b0c] (rid 122970) by nijtmans on 2012-12-29 17:05:14:

    1. icomment:
      From the Tcl_ListObjIndex documentation:
      > The reference count for the list element is not incremented;
      > the caller must do that if it needs to retain a pointer to the element.
      In this case, the caller doesn't retain a pointer to the element,
      it only passes it to Tcl_ListObjReplace. After that, objP
      is not used any more. The function Tcl_ListObjReplace
      is retaining a pointer to the element (in the list
      representation), so this function should (and does)
      increment the refCount.
      
      
    2. login: "nijtmans"
    3. mimetype: "text/plain"
    4. username: "nijtmans"
  19. Ticket change [66328101d1] (rid 102662) by ferrieux on 2012-12-29 20:50:34:

    1. icomment:
      FWIW, I agree with Jan's analysis on all points.
      
      Re the naked refcount-- , I'm still wondering whether it shouldn't have a more explicit macro (like TclDecrRefCountNoDeleteAtZero : that wasn't a joke), for two reasons:
       
       (1) the need to use it, though rare, cannot be worked around; it is a consequence of the unfortunate choice create-at-zero mentioned earlier in this thread
      
       (2) A macro keeps the abstraction level for source compat (e.g. in case we want to steal a few bits in the refcount)
      
      
    2. login: "ferrieux"
    3. mimetype: "text/plain"
    4. username: "ferrieux"
  20. Ticket change [88887a0104] (rid 131882) by nijtmans on 2012-12-30 01:23:14:

    1. icomment:
      See:
      <https://sourceforge.net/tracker/?func=detail&aid=3598875&group_id=10894&atid=360894>
      
      
    2. login: "nijtmans"
    3. mimetype: "text/plain"
    4. username: "nijtmans"
  21. Ticket change [1e94b42189] (rid 90999) by dgp on 2013-01-02 23:09:43:

    1. closedate changed to: "2013-01-02T16:09:43"
    2. closer changed to: "dgp"
    3. icomment:
      
      
      allow_comments - 1
      
      Looking again in a new year, I have no compelling
      objections to keep the patch everywhere it is committed.
      
      I do like to have demos and tests and maintainer review
      as a general matter, even with no serious harm here.
      
      
      
    4. login: "dgp"
    5. mimetype: "text/plain"
    6. status changed to: "Closed"
    7. username: "dgp"
  22. Ticket change [8629961878] (rid 131453) by nijtmans on 2013-01-03 14:59:24:

    1. icomment:
      >I do like to have demos and tests and maintainer review
      >as a general matter, even with no serious harm here.
      A Demo is given already by Ashok, I'm working on integrating
      Ashok's test case in the test suite. Tried to use the
      existing "testobj" command for this, but is looks like
      that's not possible. Upcoming.
      
      
    2. login: "nijtmans"
    3. mimetype: "text/plain"
    4. username: "nijtmans"
  23. Ticket change [9a5d88f52c] (rid 110932) by nijtmans on 2013-01-03 18:00:29:

    1. icomment:
      
      
      allow_comments - 0
      
      Simplified test case now added to all branches.
      
      The test-case doesn't crash on Windows (with
      the original Tcl_ListObjReplace implementation)
      but it returns a random object, enough to
      make the test fail. I guess it depends much on
      the memory allocator that Tcl is compiled with.
      (I used the Threaded memory allocator)
      
      
    2. login: "nijtmans"
    3. mimetype: "text/plain"
    4. username: "nijtmans"