This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

EK-TM4C1294XL: Fix for GetFieldValueString function in json.c

Part Number: EK-TM4C1294XL

Tool/software:

In the related question, I had an issue with the function GetFieldValueString included in json.c example/template from TI. We finally discovered the issue with related to the initial search for the double quote marks. Depending on how the JSON was structured on the sending side, it might add character (space, tab, return) before the quotes ". This would fail the first if-statement. This seemed to happen even when I tried to eliminate all space, returns, ect on the sending side. Therefore, I made a slight modification to the code. Instead of returning -1 and failing if it does not find double quotes at the start, the pointer will move to the next character until it finds quote marks. I have included an arbitrary limit of 20 characters before it gives up. This should prevent infinite loops or running out of bounds if there are no double quotes to be found. 

Code below. I've tested it, and it appears to work (at least for my goals. 

/******************************************************************************
 *
 * This function searches for a value in a JSON item as quoted value.  These
 * values are quoted values so if the number is not a quoted value this
 * function returns -1.  If the quoted value is found then it is returned in
 * the pcDataDest array.
 *
 *****************************************************************************/
int32_t
GetFieldValueString(tBufPtr *psBufPtr, char *pcDataDest, uint32_t ui32SizeDest) {
    int32_t i32OutIdx;
    uint8_t Idx_counter = 0; /* Counter to prevent infinite loop. */

    while (Idx_counter < 20) {
    /* String value starts with double quote " but this may not always be the
     * first character. Look for " and then move on. */
    if (BufData8Get(psBufPtr) == '"') {
        /*Found start of the string with double quote. Reset the counter. Move
         * to the next step*/
        Idx_counter = 0;
        break;
    }
    else {
    /*move pointer to next character. Searching for double quote mark.*/
        BufPtrInc(psBufPtr, 1);
        /* Fail safe against infinite loop. */
        Idx_counter++;
    }

}

    /* Skip the initial " char. */

    if (BufPtrInc(psBufPtr, 1) != 1) {
        return(-1);
    }

    for (i32OutIdx = 0; i32OutIdx < ui32SizeDest;) {
        /* Either a '}', ',', or '"' ends an item. */
        if ((BufData8Get(psBufPtr) == '}') ||
            (BufData8Get(psBufPtr) == ',') ||
            (BufData8Get(psBufPtr) == '"'))
            {
                /* Null terminate the string and return. */
                pcDataDest[i32OutIdx] = 0;
                return(i32OutIdx);
            }

        /* Continue copying chars into the destination buffer. */
        pcDataDest[i32OutIdx] = BufData8Get(psBufPtr);

        /* These can occur in the response string and need to be ignored. */
        if ((pcDataDest[i32OutIdx] == '\r') || (pcDataDest[i32OutIdx] == '\n')) {
            pcDataDest[i32OutIdx] = 0;
        } else {
            i32OutIdx++;
        }

        if (BufPtrInc(psBufPtr, 1) != 1) {
            break;
        }
    }

    /* Make sure to null terminate inside the current string. */
    if (i32OutIdx == ui32SizeDest) {
        pcDataDest[i32OutIdx - 1] = 0;
        return(i32OutIdx - 1);
    }
    return(-1);
}

-----

Devon