Changeset 725 for trunk/src


Ignore:
Timestamp:
May 10, 2007 8:00:44 PM (17 years ago)
Author:
mmc
Message:

Fixed the Rappture::encoding::is function to properly detect embedded
nulls within a string and classify the string as "binary".

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpEncode.cc

    r675 r725  
    3333isbinary(const char* buf, int size)
    3434{
    35     int index = 0;
    36 
    37     if (size == -1) {
     35    if (size < 0) {
    3836        size = strlen(buf);
    3937    }
    4038
    41     for (index = 0; index < size; index++) {
    42         if (((buf[index] >= '\000') && (buf[index] <= '\010')) ||
    43             ((buf[index] >= '\013') && (buf[index] <= '\014')) ||
    44             ((buf[index] >= '\016') && (buf[index] <= '\037')) ||
    45             ((buf[index] >= '\177') && (buf[index] <= '\377')) ) {
     39    for (int index = 0; index < size; index++) {
     40        int c = *buf++;
     41        if (((c >= '\000') && (c <= '\010')) ||
     42            ((c >= '\013') && (c <= '\014')) ||
     43            ((c >= '\016') && (c <= '\037')) ||
     44            ((c >= '\177') && (c <= '\377')) ) {
    4645            // data is binary
    4746            return index+1;
    4847        }
    49 
    5048    }
    5149    return 0;
  • trunk/src/tcl/src/RpEncodeTclInterface.cc

    r684 r725  
    7878
    7979int
    80 RpTclEncodingIs     (   ClientData cdata,
    81                         Tcl_Interp *interp,
    82                         int objc,
    83                         Tcl_Obj *const objv[]  )
     80RpTclEncodingIs (ClientData cdata, Tcl_Interp *interp,
     81    int objc, Tcl_Obj *const objv[])
    8482{
    85     const char* type     = NULL; // type of data being checked
    86     const char* buf      = NULL; // buffer to be checked
    87     const char* cmdName  = NULL;
    88 
    89     int typeLen          = 0;
    90     int bufLen           = 0; // length of user provided buffer
    91     int nextarg          = 0;  // next argument
    92 
    9383    Tcl_ResetResult(interp);
    94 
    95     cmdName = Tcl_GetString(objv[nextarg++]);
    9684
    9785    // parse through command line options
     
    9987        Tcl_AppendResult(interp,
    10088            "wrong # args: should be \"",
    101             cmdName," binary <string>\"",
     89            Tcl_GetString(objv[0])," binary <string>\"",
    10290            (char*)NULL);
    10391        return TCL_ERROR;
    10492    }
    10593
    106     type = Tcl_GetStringFromObj(objv[nextarg++],&typeLen);
    107     buf = Tcl_GetStringFromObj(objv[nextarg++],&bufLen);
    108 
    109     if (strncmp(type,"binary",typeLen) == 0) {
     94    const char *type = Tcl_GetString(objv[1]);
     95
     96    int bufLen;
     97    const char *buf = (const char*) Tcl_GetByteArrayFromObj(objv[2],&bufLen);
     98
     99    if (strcmp(type,"binary") == 0) {
    110100        if (Rappture::encoding::isbinary(buf,bufLen) != 0) {
    111101            // non-ascii character found, return yes
    112             Tcl_AppendResult(interp, "yes",(char*)NULL);
    113             return TCL_OK;
     102            Tcl_AppendResult(interp, "yes", (char*)NULL);
     103        } else {
     104            Tcl_AppendResult(interp, "no",(char*)NULL);
    114105        }
    115     }
    116     else {
    117         Tcl_AppendResult(interp, "bad option \"", type,
    118                 "\": should be binary",
    119                 (char*)NULL);
    120         return TCL_ERROR;
    121 
    122     }
    123 
    124     // default return
    125     // no binary characters found, return no
    126     Tcl_AppendResult(interp, "no",(char*)NULL);
    127     return TCL_OK;
     106        return TCL_OK;
     107    }
     108    Tcl_AppendResult(interp, "bad option \"", type,
     109            "\": should be binary",
     110            (char*)NULL);
     111    return TCL_ERROR;
    128112}
    129113
  • trunk/src/tcl/tests/encode.test

    r684 r725  
    5252    list [catch {Rappture::encoding::is binary binary "hi"} msg] $msg
    5353} {1 {wrong # args: should be "Rappture::encoding::is binary <string>"}}
     54
     55test is-1.2.2 {Rappture::encoding::is, with an embedded null} {
     56    list [catch {Rappture::encoding::is binary "x\000x"} msg] $msg
     57} {0 yes}
    5458
    5559#----------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.