我目前正在开展一个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递增,所以我不明白为什么这会导致seg错误。
感谢您提供的任何帮助。
答案 0 :(得分:1)
看起来你缺少一个参数。格式字符串包含15个争论而你给它14.所以outboundLegCounter
被视为%s
。
取消引用整数肯定会产生段错误。
答案 1 :(得分:1)
您在此行中缺少epochBuffer
:
dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(outboundLeg->cleardownCause), outboundLegCounter);