潜在的内存溢出,但不确定是什么原因造成的

问题描述

我看到一个潜在的溢出:streamBuffer是一个结构对象(属于FreeRTOS lib),在执行OutputToSerial()中的以下行后,我看到streamBuffer.xHead的值是设置为一个非常大的值,即使当时没有被修改。

LONG_TO_STR(strData,txStr);
  • 请注意,当我多次致电nRF24_ReadReg()时,我没有任何问题。
  • 通常,我经常看到printf不能打印出正在打印的整个文本(在我看到潜在的溢出时间之前),而是错过了一些字符。

有什么方法可以更好地了解原因?我看不到寄存器中有任何硬故障或任何东西...

作为参考,以下是该结构的定义:

typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */
{
    volatile size_t xTail;              /* Index to the next item to read within the buffer. */
    volatile size_t xHead;              /* Index to the next item to write within the buffer. */

    size_t xLength;                     /* The length of the buffer pointed to by pucBuffer. */
    size_t xTriggerLevelBytes;          /* The number of bytes that must be in the stream buffer before a task that is waiting for data is unblocked. */
    volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data,or NULL if no tasks are waiting. */
    volatile TaskHandle_t xTaskWaitingToSend;   /* Holds the handle of a task waiting to send data to a message buffer that is full. */
    uint8_t *pucBuffer;                 /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
    uint8_t ucFlags;

    #if ( configUSE_TRACE_FACILITY == 1 )
        UBaseType_t uxStreamBufferNumber;       /* Used for tracing purposes. */
    #endif
} StreamBuffer_t;
// file.c
#define PRI_UINT64_C_Val(value) ((unsigned long) (value>>32)),((unsigned long)value)
#define LONG_TO_STR(STR,LONG_VAL) (sprintf(STR,"%lx%lx",PRI_UINT64_C_Val(LONG_VAL)))

unsigned long long concatData(uint8_t *arr,uint8_t size)
{
    long long unsigned value = 0;
    for (uint8_t i = 0; i < size; i++)
    {
        value <<= 8;
        value |= arr[i];
    }
    return value;
}

void nRF24_ReadReg(nrfl2401 *nrf,uint8_t reg,const uint8_t rxSize,uint8_t *rxBuffer,char *text)
{
    uint8_t txBuffer[1] = {0};
    uint8_t spiRxSize = rxSize;

    if (reg <= nRF24_CMD_W_REG)
    {
        txBuffer[0] = nRF24_CMD_R_REG | (reg & nRF24_R_W_MASK);
        spiRxSize++;
    }
    else
    {
        txBuffer[0] = reg;
    }

    nRF24_SendCommand(nrf,txBuffer,rxBuffer,spiRxSize);

    OutputToSerial(txBuffer,spiRxSize,text);
}

void OutputToSerial(uint8_t *writeBuffer,uint8_t *readBuffer,uint8_t size,char *text)
{
    char strData[100] = {0},rxStrData[100] = {0};
    long long unsigned txStr = concatData(writeBuffer,size);
    long long unsigned rxStr = concatData(readBuffer,size);
    LONG_TO_STR(strData,txStr);              // POTENTIAL ERROR.....!
    LONG_TO_STR(rxStrData,rxStr);

    char outputMsg[60] = {0};
    strcpy(outputMsg,text);
    strcat(outputMsg,":          0x%s ----------- 0x%s\n");

    printf (outputMsg,strData,rxStrData);
}

// main.c
StreamBufferHandle_t streamBuffer;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)