IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6278


Ignore:
Timestamp:
Feb 1, 2006, 10:40:56 AM (20 years ago)
Author:
drobbin
Message:

Added error handling to psString for NULL string inputs.

Location:
trunk/psLib
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psString.c

    r6218 r6278  
    1313 *  @author David Robbins, MHPCC
    1414 *
    15  *  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
    16  *  @date $Date: 2006-01-27 01:49:05 $
     15 *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-02-01 20:40:56 $
    1717 *
    1818 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3131psString psStringCopy(const char *string)
    3232{
     33    PS_ASSERT_PTR_NON_NULL(string, NULL);
    3334    // Allocate memory using psAlloc function
    3435    // Copy input string to memory just allocated
    3536    // Return the copy
    36     return strcpy(psAlloc(strlen(string) + 1), string);
     37    // Pass through NULL values
     38    return string ? strcpy(psAlloc(strlen(string) + 1), string) : NULL;
    3739}
    3840
     
    4042                       unsigned int nChar)
    4143{
     44    PS_ASSERT_PTR_NON_NULL(string, NULL);
    4245    char *returnValue = NULL;
    4346
     
    5356    // Copy input string to memory allocated up to nChar characters
    5457    // Return the copy
    55     returnValue = strncpy(psAlloc((size_t) nChar + 1), string, (size_t) nChar);
     58    // Pass through NULL values
     59    returnValue =
     60              string ? strncpy(psAlloc((size_t) nChar + 1), string, (size_t) nChar)
     61              : NULL;
    5662
    5763    // Ensure the last byte is NULL character
     
    6672                       ...)
    6773{
     74    PS_ASSERT_PTR_NON_NULL(format, 0);
    6875    va_list         args;
    6976    size_t          length;             // complete string length (sans \0)
     
    113120                        ...)
    114121{
     122    PS_ASSERT_PTR_NON_NULL(format, 0);
    115123    va_list         args;
    116124    size_t          length;             // complete string length (sans \0)
  • trunk/psLib/src/sys/psString.h

    r6201 r6278  
    1414 *  @author David Robbins, MHPCC
    1515 *
    16  *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2006-01-26 05:31:54 $
     16 *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2006-02-01 20:40:56 $
    1818 *
    1919 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3838/** Copies the input string
    3939 *
    40  *  This function shall allocate memory to the length of the input string
    41  *  plus one and copy the input string to the newly allocated memory.
     40 *  This function shall allocate memory to the length of the input string plus
     41 *  one and copy the input string to the newly allocated memory.  If 'string'
     42 *  is 'NULL' then 'NULL' is returned.
    4243 *
    4344 *  @return psString:      Copy of input string
     
    5556 *  string will be a substring of the input string.  If the input string
    5657 *  is smaller than nChar bytes then the remaining bytes allocated will
    57  *  be set to NULL.
     58 *  be set to NULL.  If 'string' is 'NULL' then 'NULL' is returned.
    5859 *
    5960 *  @return  psString:   Copy of input string
  • trunk/psLib/src/types/psMetadata.c

    r6251 r6278  
    1212 *  @author Ross Harman, MHPCC
    1313 *
    14  *  @version $Revision: 1.97 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2006-01-30 20:28:33 $
     14 *  @version $Revision: 1.98 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2006-02-01 20:40:56 $
    1616 *
    1717 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    264264    case PS_DATA_STRING:
    265265        // Perform copy of input strings
    266         metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
     266        {
     267            char *string = va_arg(argPtr, char *);
     268            metadataItem->data.V =
     269                string ? psStringNCopy(string, MAX_STRING_LENGTH)
     270                : NULL;
     271        }
    267272        break;
    268273    case     PS_DATA_ARRAY:                     // psArray
  • trunk/psLib/test/sys/tst_psString.c

    r6218 r6278  
    2020 *  @author  Eric Van Alst, MHPCC
    2121 *
    22  *  @version $Revision: 1.3 $  $Name: not supported by cvs2svn $
    23  *  @date  $Date: 2006-01-27 01:49:05 $
     22 *  @version $Revision: 1.4 $  $Name: not supported by cvs2svn $
     23 *  @date  $Date: 2006-02-01 20:40:56 $
    2424 *
    2525 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    5252
    5353static psS32 testStrSplit00(void);
     54static psS32 testNULLStrings(void);
    5455
    5556testDescription tests[] = {
     
    7273                              {testStrPrepend03,14, "Test prepend null-op", 0, false},
    7374                              {testStrSplit00,15, "Test String Splitting", 0, false},
     75                              {testNULLStrings,666, "Test NULL String Error Handling", 0, false},
    7476                              {NULL}
    7577                          };
     
    619621}
    620622
     623static psS32 testNULLStrings(void)
     624{
     625    psString nullTest = NULL;
     626    psString output = NULL;
     627    ssize_t outSize = 0;
     628    char** nullDest = NULL;
     629    char** test;
     630    //psStringCopy should return NULL for NULL input string
     631    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
     632    output = psStringCopy(nullTest);
     633    if (output != NULL) {
     634        psError(PS_ERR_BAD_PARAMETER_NULL, false,
     635                "psStringCopy failed to return NULL for NULL input string.\n");
     636        return 1;
     637    }
     638    //psStringNCopy should return NULL for NULL input string
     639    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
     640    output = psStringNCopy(nullTest, 100);
     641    if (output != NULL) {
     642        psError(PS_ERR_BAD_PARAMETER_NULL, false,
     643                "psStringNCopy failed to return NULL for NULL input string.\n");
     644        return 2;
     645    }
     646
     647    //psStringAppend should return 0 for NULL input destination
     648    outSize = psStringAppend(nullDest, "");
     649    if (outSize != 0) {
     650        psError(PS_ERR_BAD_PARAMETER_NULL, false,
     651                "psStringAppend failed to return 0 for NULL input destination.\n");
     652        return 3;
     653    }
     654    //psStringAppend should return 0 for NULL input format
     655    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
     656    outSize = psStringAppend(test, nullTest);
     657    if (outSize != 0) {
     658        psError(PS_ERR_BAD_PARAMETER_NULL, false,
     659                "psStringAppend failed to return 0 for NULL input format.\n");
     660        return 4;
     661    }
     662    //psStringPrepend should return 0 for NULL input destination
     663    outSize = psStringPrepend(nullDest, " ");
     664    if (outSize != 0) {
     665        psError(PS_ERR_BAD_PARAMETER_NULL, false,
     666                "psStringPrepend failed to return 0 for NULL input destination.\n");
     667        return 3;
     668    }
     669    //psStringPrepend should return 0 for NULL input format
     670    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
     671    outSize = psStringPrepend(test, nullTest);
     672    if (outSize != 0) {
     673        psError(PS_ERR_BAD_PARAMETER_NULL, false,
     674                "psStringPrepend failed to return 0 for NULL input format.\n");
     675        return 4;
     676    }
     677    //psStringSplit should return NULL for NULL input string
     678    psList *nullList = NULL;
     679    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
     680    nullList = psStringSplit(nullTest, ",");
     681    if (nullList != NULL) {
     682        psError(PS_ERR_BAD_PARAMETER_NULL, false,
     683                "psStringSplit failed to return NULL for NULL input string.\n");
     684        return 5;
     685    }
     686    //psStringSplit should return NULL for NULL input splitter
     687    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
     688    nullList = psStringSplit("Hello World", nullTest);
     689    if (nullList != NULL) {
     690        psError(PS_ERR_BAD_PARAMETER_NULL, false,
     691                "psStringSplit failed to return NULL for NULL input splitter.\n");
     692        return 6;
     693    }
     694
     695    return 0;
     696}
     697
  • trunk/psLib/test/sys/verified/tst_psString.stderr

    r6218 r6278  
    6868\**********************************************************************************/
    6969
     70<DATE><TIME>|<HOST>|E|psStringAppend (FILE:LINENO)
     71    Unallowable operation: format is NULL.
     72<DATE><TIME>|<HOST>|E|psStringAppend (FILE:LINENO)
     73    Unallowable operation: format is NULL.
    7074
    7175---> TESTPOINT PASSED (psString{Test append NULL handling} | tst_psString.c)
     
    104108\**********************************************************************************/
    105109
     110<DATE><TIME>|<HOST>|E|psStringPrepend (FILE:LINENO)
     111    Unallowable operation: format is NULL.
     112<DATE><TIME>|<HOST>|E|psStringPrepend (FILE:LINENO)
     113    Unallowable operation: format is NULL.
    106114
    107115---> TESTPOINT PASSED (psString{Test prepend NULL handling} | tst_psString.c)
     
    146154---> TESTPOINT PASSED (psString{Test String Splitting} | tst_psString.c)
    147155
     156/***************************** TESTPOINT ******************************************\
     157*             TestFile: tst_psString.c                                             *
     158*            TestPoint: psString{Test NULL String Error Handling}                  *
     159*             TestType: Positive                                                   *
     160\**********************************************************************************/
     161
     162<DATE><TIME>|<HOST>|I|testNULLStrings
     163    Following should generate error message
     164<DATE><TIME>|<HOST>|E|psStringCopy (FILE:LINENO)
     165    Unallowable operation: string is NULL.
     166<DATE><TIME>|<HOST>|I|testNULLStrings
     167    Following should generate error message
     168<DATE><TIME>|<HOST>|E|psStringNCopy (FILE:LINENO)
     169    Unallowable operation: string is NULL.
     170<DATE><TIME>|<HOST>|I|testNULLStrings
     171    Following should generate error message
     172<DATE><TIME>|<HOST>|E|psStringAppend (FILE:LINENO)
     173    Unallowable operation: format is NULL.
     174<DATE><TIME>|<HOST>|I|testNULLStrings
     175    Following should generate error message
     176<DATE><TIME>|<HOST>|E|psStringPrepend (FILE:LINENO)
     177    Unallowable operation: format is NULL.
     178<DATE><TIME>|<HOST>|I|testNULLStrings
     179    Following should generate error message
     180<DATE><TIME>|<HOST>|E|psStringSplit (FILE:LINENO)
     181    Unallowable operation: string is NULL.
     182<DATE><TIME>|<HOST>|I|testNULLStrings
     183    Following should generate error message
     184<DATE><TIME>|<HOST>|E|psStringSplit (FILE:LINENO)
     185    Unallowable operation: splitters is NULL.
     186
     187---> TESTPOINT PASSED (psString{Test NULL String Error Handling} | tst_psString.c)
     188
Note: See TracChangeset for help on using the changeset viewer.