Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: f9a87c64abc4822f61a2c7946b365ad81dc9cdb7c89b829ca830fc70469a094b
Ticket: 26870862f0e8884eaec02c5a3bbc4caf2431a310
Wrong sentinel in Tcl_SetErrorCode usage
User & Date: gustafn3 on 2023-10-20 07:53:09
Changes

  1. icomment:
    Passing NULL after the last typed argument to a variadic function is not
    recommended either, since this leads to undefined behaviour. 
    This construct will lead e.g. to the following error flagged by cppcheck:
    
    ==========================================================================================
    generic/tclBinary.c:403:52: portability: Passing NULL after the last 
    typed argument to a variadic function leads to undefined behaviour.
    
    The C99 standard, in section 7.15.1.1, states that if the type used 
    by va_arg() is not compatible with the type of the actual next argument 
    (as promoted according to the default argument promotions), the behavior is undefined.
    
    The value of the NULL macro is an implementation-defined null pointer 
    constant (7.17), which can be any integer constant expression with the value 0, 
    or such an expression casted to (void*) (6.3.2.3). This includes values like 
    0, 0L, or even 0LL.
    
    In practice on common architectures, this will cause real crashes if 
    sizeof(int) != sizeof(void*), and NULL is defined to 0 or any other 
    null pointer constant that promotes to int.
    
    To reproduce you might be able to use this little code example on 64bit
    platforms. If the output includes "ERROR", the sentinel had only 4 out of 
    8 bytes initialized to zero and was not detected as the final argument to 
    stop argument processing via va_arg(). Changing the 0 to (void*)0 or 0L 
    will make the "ERROR" output go away.
    
      Tcl_SetErrorCode(interp, "TCL", "API", "OUTDATED", NULL);
    ==========================================================================================
    
    In NaviServer such occurrences have been replaced by using "(char *)0L" 
    instead of "NULL" after the last typed argument. Here are some examples 
    concerning Tcl_SetErrorCode():
    
    ==========================================================================================
    nsd/tcljob.c:                    Tcl_SetErrorCode(interp, "NS_TIMEOUT", (char *)0L);
    nsd/tcljob.c:                    Tcl_SetErrorCode(interp, jobPtr->errorCode, (char *)0L);
    nsd/tcljob.c:                    Tcl_SetErrorCode(interp, "NS_TIMEOUT", (char *)0L);
    nsd/tcljob.c:        Tcl_SetErrorCode(interp, "ECANCEL", (char *)0L);
    nsd/tclobjv.c:        Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "INDEX", msg, key, (char *)0L);
    nsd/tclvar.c:                    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "KEY", keyString, (char *)0L);
    nsd/tclvar.c:                Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "KEY", keyString, (char *)0L);
    nsd/tclvar.c:                Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "KEY", keyString, (char *)0L);
    nsd/tclvar.c:            Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "KEY", keyString, (char *)0L);
    nsd/tclvar.c:                                    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "DICT",
    nsd/tclvar.c:        Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "ARRAY", arrayName, (char *)0L);
    nsd/tls.c:        Tcl_SetErrorCode(interp, "OPENSSL", errorBuf, (char *)0L);
    nsd/urlencode.c:            Tcl_SetErrorCode(interp, "NS_INVALID_UTF8", NULL);
    ==========================================================================================
    
    If there is interest, i can provide a fixed branch for such issues.
    
  2. login: "gustafn3"
  3. mimetype: "text/plain"