0

我目前正在做一个 C 项目,我遇到了一个我不明白的相当奇怪的问题。

我正在使用 asprintf 构建一个 SQL 语句,该语句工作正常,直到我将一个 int 变量添加到字符串中,然后它会导致分段错误。以下是我为该功能提供的代码。

int drilldownSetRowData(callLogSearchDataStruct * callLogSearchData, int dataRow, MYSQL *HandleDB, long inboundEpochTimeStamp)
{
    char * inboundSql = NULL;
    char * sql = NULL;
    int sqlLen = 0;
    char * tempSql = NULL;
    char * outboundSql = NULL;

    char epochBuffer[11];
    int outboundLegCounter = 0;
    callLogSearchOutboundStruct * outboundLeg = NULL;
    if (dataRow == -1)
    {
        return 0;
    }
    char durationBuffer[8];

    snprintf(durationBuffer, sizeof(durationBuffer), "%.1f", callLogSearchData[dataRow].duration);
    snprintf(epochBuffer, sizeof(epochBuffer), "%ld", inboundEpochTimeStamp);

    asprintf(&inboundSql, "INSERT INTO DataTable VALUES (%i, %i, '%s', '%s', %i),"
        "(%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i),"
        "(%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i)",
        dataRow, D_DATE, callLogSearchData[dataRow].date, epochBuffer, outboundLegCounter,
        dataRow, D_TIME, callLogSearchData[dataRow].time, epochBuffer, outboundLegCounter,
        dataRow, D_APARTY, callLogSearchData[dataRow].aParty, epochBuffer, outboundLegCounter,
        dataRow, D_BPARTY, callLogSearchData[dataRow].bParty, epochBuffer, outboundLegCounter,
        dataRow, D_DURATION, durationBuffer, epochBuffer,outboundLegCounter,
        dataRow, D_RESULT, callLogSearchData[dataRow].cleardownCause, epochBuffer, outboundLegCounter);

    for (outboundLeg = callLogSearchData[dataRow].outboundLegs; outboundLeg != NULL && outboundLeg->target != NULL; outboundLeg = outboundLeg->nextLeg)
    {
        outboundLegCounter++;
        snprintf(durationBuffer, sizeof(durationBuffer), "%.1f", outboundLeg->duration);

        if (outboundSql == NULL)
        {
            printf("outboundSql is NULL\n");
            asprintf(&tempSql, "(%i, %i, '%s', '%s', 6),"
                "(%i, %i, '%s', '%s', 7), (%i, %i, '%s', '%s', 8)",
                dataRow, D_TARGET, outboundLeg->target, epochBuffer,
                dataRow, D_TARGET_DURATION, durationBuffer, epochBuffer,
                dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(outboundLeg->cleardownCause));
        }
        else
        {
            printf("outboundSql is not NULL\n");
            asprintf(&tempSql, "%s, (%i, %i, '%s', '%s', %i),"
                    "(%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i)",
                outboundSql, dataRow, D_TARGET, outboundLeg->target, epochBuffer, outboundLegCounter,
                dataRow, D_TARGET_DURATION, durationBuffer, epochBuffer, outboundLegCounter,
                dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(callLogSearchData->cleardownCause), epochBuffer, outboundLegCounter);
        }

    }
    outboundSql = tempSql;
    if (outboundSql != NULL)
    {
        sqlLen = asprintf(&sql, "%s, %s", inboundSql, outboundSql);
    }
    else
    {
        sqlLen = asprintf(&sql, "%s", inboundSql);
    }
    SL_DebugAll(DBG_INFO, sql);
    if ((mysql_real_query(HandleDB, sql, sqlLen))) return 1;

    return 0;
}

问题出在以下几行:

if (outboundSql == NULL)
        {
            printf("outboundSql is NULL\n");
            asprintf(&tempSql, "(%i, %i, '%s', '%s', %i),"
                "(%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i)",
                dataRow, D_TARGET, outboundLeg->target, epochBuffer, outboundLegCounter
                dataRow, D_TARGET_DURATION, durationBuffer, epochBuffer, outboundLegCounter,
                dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(outboundLeg->cleardownCause), outboundLegCounter);
        }

如果我从 asprintf 中删除 outboundLegCounter 参数并将一个 int 值硬编码到字符串中(替换每行插入末尾的 %i),则程序可以正常工作,但是使用该参数会引发分段错误。

正如您在代码中看到的,outboundLegCounter 设置为 0,循环中发生的第一件事是 outboundLegCounter 递增,所以我不明白为什么这会导致段错误。

感谢您的任何帮助,您可以提供。

4

2 回答 2

1

epochBuffer在这一行缺少一个:

dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(outboundLeg->cleardownCause), outboundLegCounter);
于 2013-11-08T10:29:41.160 回答
1

看起来您缺少一个参数。格式字符串包含 15 个参数,你给它 14。所以它outboundLegCounter被视为%s.

取消引用整数肯定会产生段错误。

于 2013-11-08T10:27:56.410 回答