FreeWRL,“格式不是字符串文字,没有格式参数”

时间:2014-01-09 12:23:51

标签: c android-ndk printf

所以我正在尝试构建freewrl android库,这是在原生的android代码中,这意味着在C中。我不熟悉C,并认为既然包含了构建脚本,它应该开箱即用。 - 它不是。 在解决了一些小问题后,这就是我现在正在看的内容:

/opt/freewrl/Android/jni/../../freex3d/src/lib/main/ConsoleMessage.c: In function 'fwvsnprintf':
/opt/freewrl/Android/jni/../../freex3d/src/lib/main/ConsoleMessage.c:333:4: error: format not a string literal and no format arguments [-Werror=format-security]
cc1: some warnings being treated as errors
make: *** [/opt/freewrl/Android/obj/local/armeabi/objs/FreeWRL/__/__/freex3d/src/lib/main/ConsoleMessage.o] Error 1

ConsoleMessage.c中与之相关的代码如下:

count += sprintf(tempbuf, format);/* printf it verbatim             */

以下构造或功能的一部分:

int fwvsnprintf(char *buffer,int buffer_length, const char *fmt, va_list ap)
{
    int i,j,count;
    //char tempbuf[STRING_LENGTH];
    //char format[STRING_LENGTH];
    char *tempbuf;
    char *format;
    char c;
    double d;
    unsigned u;
    char *s;
    void *v;
    tempbuf = malloc(buffer_length);
    format = malloc(buffer_length);
    count = 0;
    buffer[0] = '\0';
    while (*fmt) 
    {
        tempbuf[0] = '\0';
        for (j = 0; fmt[j] && fmt[j] != '%'; j++) {
            format[j] = fmt[j]; /* not a format string  */
        }

        if (j) {
            format[j] = '\0';
            count += sprintf(tempbuf, format);/* printf it verbatim             */
            fmt += j;
        } else {
            for (j = 0; !isalpha(fmt[j]); j++) {     /* find end of format specifier */
                format[j] = fmt[j];
                if (j && fmt[j] == '%')             /* special case printing '%'        */
                    break;
            }
            format[j] = fmt[j];         /* finish writing specifier      */
            format[j + 1] = '\0';           /* don't forget NULL terminator */
            fmt += j + 1;

            switch (format[j]) {             /* cases for all specifiers         */
            case 'd':
            case 'i':                       /* many use identical actions    */
                i = va_arg(ap, int);         /* process the argument     */
                count += sprintf(tempbuf, format, i); /* and printf it       */
                break;
            case 'o':
            case 'x':
            case 'X':
            case 'u':
                u = va_arg(ap, unsigned);
                count += sprintf(tempbuf, format, u);
                break;
            case 'c':
                c = (char) va_arg(ap, int);     /* must cast!            */
                count += sprintf(tempbuf, format, c);
                break;
            case 's':
                s = va_arg(ap, char *);
                /* limit string to a certain length */
                if ((strlen(s) + count) > buffer_length) {
                    char tmpstr[100];
                    int ltc;
                    ltc = (int) strlen(s);
                    if (ltc>80) ltc=80;
                    strncpy (tmpstr, s, ltc);
                    tmpstr[ltc] = '.'; ltc++;
                    tmpstr[ltc] = '.'; ltc++;
                    tmpstr[ltc] = '.'; ltc++;
                    tmpstr[ltc] = '\0';

                    count += sprintf (tempbuf, format, tmpstr);
                } else count += sprintf(tempbuf, format, s);
                break;
            case 'f':
            case 'e':
            case 'E':
            case 'g':
            case 'G':
                d = va_arg(ap, double);
                count += sprintf(tempbuf, format, d);
                break;
            case 'p':
                v = va_arg(ap, void *);
                count += sprintf(tempbuf, format, v);
                break;
            case 'n':
                count += sprintf(tempbuf, "%d", count);
                break;
            case '%':
                count += sprintf(tempbuf, "%%");
                break;
            default:
                ERROR_MSG("ConsoleMessage: invalid format specifier: %c\n", format[j]);
            }
        }
        if( (strlen(tempbuf) + strlen(buffer)) < (buffer_length) -10) 
        {
            strcat (buffer,tempbuf);
        }
    }
    free(tempbuf);
    free(format);
    return 1;
}

据我所见,format填充了fmt的内容,这是一个参数,直到读出%为止。 tempbuf似乎是'\ 0',所以对我来说是一个空字节。 但就我而言,这是我所能得到的,所以我很感激任何帮助,因为我正在努力建立这个库。 提前谢谢。

1 个答案:

答案 0 :(得分:7)

根据错误,似乎在您的makefile默认情况下启用了-Werror=format-security标记,并且不会导致字符串格式出现任何安全问题,例如printf或{{ 1}} function.So警告在源代码中被视为错误。因此,如果您不担心安全性,请禁用它。或者请在代码中的任何地方进行更改,如下所示,可能会删除错误。

scanf

为安全起见count += sprintf(tempbuf,"%s",format);/* printf it verbatim */ 喜欢

snprintf