Tcl Source Code

View Ticket
Login
Ticket UUID: 6e156333bcf233a26216e3ee3e94f2761e97a34b
Title: Documentation: Contradiction in Tcl_GetSring/Tcl_GetStringFromObj vs Tcl_UpdateStringProc
Type: Bug Created on: 2019-07-22 20:00:32
Submitter: anonymous Assigned to: dgp
Subsystem: 10. Objects Severity: Minor
Priority: 5 Medium Last modified: 2019-07-23 09:39:50
Status: Open Closed by: nobody
Resolution: None Closed on: 2019-07-23 08:03:44
Version: 8.6
Description:
Tcl_GetString documentation (http://www.tcl.tk/man/tcl8.6/TclLib/StringObj.htm) says:
[The object's string representation] is passed back as a writable pointer so
that extension author creating their own Tcl_ObjType will be able to modify 
the string representation within the Tcl_UpdateStringProc of their Tcl_ObjType.

THE UPDATESTRINGPROC FIELD in http://www.tcl.tk/man/tcl8.6/TclLib/ObjectType.htm says:
objPtr's bytes member is always NULL when it is called.

This would seem to be a contradiction: if the bytes member is NULL, then 
updateStringProc would need to be called to set it to non-null; trying to 
invoke Tcl_GetString or Tcl_GetStringFromObj would just result in a 
recursive call to updateStringProc -- unless the typePtr were temporarily 
changed to a different type.

Is that (temporary modification of the typePtr) the intended meaning?

BTW: I don't know how to change my password on this site.
User Comments:
sebres added on 2019-07-22 21:31:16:

> objPtr's bytes member is always NULL when it is called.

means BEFORE, so the updateStringProc is never called if objPtr->bytes is not NULL and vice versa (only called if it is NULL).

See tclObj.c#Tcl_GetString for more info.

Three main things are important here:

1. The handler used to update the string representation only if it is NULL (and never if it is not NULL);

2. It must ALWAYS update it (so set to not NULL):

  • either set it to some buffer (allocated by ckalloc) and fill with NTS corresponding your internal representation and set objPtr->length to proper length of that, so follow EIAS.
  • or set it to tclEmptyStringRep if you don't "need" to follow EIAS (and if you have tcl-internal's or obtained tclEmptyStringRep pointer from some function).
    If you would not create it, so after call of updateStringProc it is still NULL - then the only thing you'd get hereafter is a SEGFAULT (or at least panic with "failed to create a valid string rep" on tcl before 9.0).
3. Once created, it remains quasi immutable (at least it should), you can free the string representation (e. g. using Tcl_InvalidateStringRep) but only if the object is not shared or you have it only under your control, e. g. you know all the references are yours (see my answer for such question on SO).

Thus closed as invalid, please reopen if I'm wrong.


anonymous (claiming to be [email protected]) added on 2019-07-23 08:03:44:

Your comment restates what THE UPDATESTRING FIELD says and I have no issue with that.

However, the Tcl_GetString documentation is saying something else: it says you can call Tcl_GetString from the UpdateStringProc to get a modifiable string that you can then change (presumably without changing its length or at most shortening it).

But if the bytes member is NULL when UpdateStringProc is called, and UpdateStringProc calls Tcl_GetString, then Tcl_GetString would have no recourse but to call UpdateStringProc again, leading to infinite recursion (unless the type pointer had been changed before calling Tcl_GetString).

So how is this supposed to work? (Or should the Tcl_GetString documentation be changed?)


sebres added on 2019-07-23 09:21:44:

Oh... do you mean this sentence?

If the value's UTF string representation is invalid (its byte pointer is NULL), the string representation is regenerated from the value's internal representation. The storage referenced by the returned byte pointer is owned by the value manager. It is passed back as a writable pointer so that extension author creating their own Tcl_ObjType will be able to modify the string representation within the Tcl_UpdateStringProc of their Tcl_ObjType.

Indeed, this looks a bit confusing resp. one could understand ambiguous in this context.
It was firstly introduced in check-in [b1c464dbcaf08c6d], and I'm still not quite following the meaning of that change.

Thanks! Thus reopening.


sebres added on 2019-07-23 09:39:50:

Looks like Don were competent to this change (see "bug" [500930]).
Assigned.