STRING_HANDLE STRING_construct_n()

in src/strings.c [757:802]


STRING_HANDLE STRING_construct_n(const char* psz, size_t n)
{
    STRING_HANDLE result;
    /*Codes_SRS_STRING_02_008: [If psz is NULL then STRING_construct_n shall return NULL.] */
    if (psz == NULL)
    {
        result = NULL;
        LogError("invalid arg (NULL)");
    }
    else
    {
        size_t len = strlen(psz);
        /*Codes_SRS_STRING_02_009: [If n is bigger than the size of the string psz, then STRING_construct_n shall return NULL.] */
        if (n > len)
        {
            result = NULL;
            LogError("invalig arg (n is bigger than the size of the string)");
        }
        else
        {
            STRING* str;
            if ((str = malloc(sizeof(STRING))) != NULL)
            {
                if ((str->s = malloc(n + 1)) != NULL) /*if n is SIZE_MAX then condition in line 770 is extrmeely likely true and this line is never reached*/ 
                {
                    (void)memcpy(str->s, psz, n);
                    str->s[n] = '\0';
                    result = str;
                }
                /* Codes_SRS_STRING_02_010: [In all other error cases, STRING_construct_n shall return NULL.]  */
                else
                {
                    LogError("Failure allocating value.");
                    free(str);
                    result = NULL;
                }
            }
            else
            {
                /* Codes_SRS_STRING_02_010: [In all other error cases, STRING_construct_n shall return NULL.]  */
                result = NULL;
            }
        }
    }
    return result;
}