如何去掉静态分析报告中的"tainted parameter"?

How to get rid of "tainted parameter" in static analysis report?

我正在使用 Parasoft 分析我的代码。我去这个违规:

Tainted parameter of entry point method ("inFileName") has been printed on the console

这是错误所在的代码:

static void printUsage(char *inFileName)
{
    printf("Usage: %s %s\n", inFileName, "[-h|-help|-usage]\n");
}

int main(int argc, char **argv)
{
    printUsage(argv[0]);
    return 0;
}

其中 inFileNAme 实际上是 argv[0]

如何解决违规问题或至少让 Parasoft 满意?

您可能会收到此警告,因为您没有正确清理程序参数。例如,如果你得到一个 non-terminated 字符串,printf 中的 %s 说明符会使你的程序继续读取(和打印)内存,导致未定义的行为和安全问题。

至于什么是"Tainted parameter":

In software security analysis, a value is said to be tainted if it comes from an untrusted source (outside of the program’s control) and has not been sanitized to ensure that it conforms to any constraints on its value that consumers of the value require — for example, that all strings are null-terminated.

(source)(强调我的)

为了确保你的输入值是正确的,你可以使用像strdup这样的函数...... :

static void printUsage(char *inFileName)
{
    char *inFile = strdup(inFileName);
    if (inFile == 0) {
    printf("Error with program Argument.");
    }else{
    printf("Usage: %s %s\n", inFile, "[-h|-help|-usage]\n");
    free(inFile);}
}

int main(int argc, char **argv)
{
    printUsage(argv[0]);
    return 0;
}