【问题标题】:Adding extra int variable to asprintf string causes segmentation fault.向 asprintf 字符串添加额外的 int 变量会导致分段错误。
【发布时间】:2013-11-08 10:21:42
【问题描述】:

我目前正在做一个 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 递增,所以我不明白为什么这会导致段错误。

感谢您提供的任何帮助。

【问题讨论】:

    标签: c


    【解决方案1】:

    您在这一行缺少epochBuffer

    dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(outboundLeg->cleardownCause), outboundLegCounter);
    

    【讨论】:

    • 感谢帮助,不知道看了多少遍代码都没注意到。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 2017-08-02
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多