TIP 481: <code>Tcl_GetStringFromObj()</code> with <code>size_t</code> length parameter

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Author:         Jan Nijtmans <[email protected]>
State:          Draft
Type:           Project
Vote:           Pending
Created:        27-Oct-2017
Post-History:
Keywords:       Tcl
Tcl-Version:    8.7
Tcl-Branch:     tip-481

Abstract

This TIP proposes enhancing various C API functions which having a int * parameter, to be used with a size_t * parameter as well.

Rationale

In Tcl 9, the range of various functions need to be increased. For example Tcl_GetStringFromObj now is limited to returning 32 bit for the maximum string length. This can be fixed by introducing an internal function Tcl_GetStringFromObj2() which has a size_t * parameter in stead of int *. On top of that, Tcl_GetStringFromObj is provided as a macro, which switches between the two possible functions depending on the size of the parameter.

The same is done for Tcl_GetUnicodeFromObj and Tcl_GetByteArrayFromObj.

This way, we have a compatibility layer, easing the transition to Tcl 9. In Tcl 8.7, although the parameter has type size_t, the actual length range that can be returned is actually -1 up to 4294967294. In Tcl 9, the full size_t range is available.

Specification

Add to Tcl's stub table of public C routines the following new routines

int Tcl_GetStringFromObj2(Tcl_Obj *objPtr, size_t *lengthPtr)

int Tcl_GetUnicodeFromObj2(Tcl_Obj *objPtr, size_t *lengthPtr)

int Tcl_GetByteArrayFromObj2(Tcl_Obj *objPtr, size_t *lengthPtr)

Those 3 functions do exactly the same as their existing counterparts, only the lengthPtr parameter is of type size_t in stead of int. The original 3 functions will be wrapped in a macro of the same name, so depending on the actual size of the variable where lengthPtr points to, the switch between the two function versions are done automatically. So, the new functions don't need to be used in code, the original function can be used pointing to a size_t length variable as well.

Implementation

See the tip-481 branch in Tcl's fossil repository https://core.tcl-lang.org/tcl/timeline?r=tip-481 .