2014-01-09 49 views
2

所以我试图建立freewrl android库,这是在本机android代码,这意味着在C.我不熟悉C,并且因为包含了构建脚本,所以应该开箱即用。 - 它不是。 修复了一些小问题之后,这是我看现在:FreeWRL,“格式不是字符串文字和没有格式参数”

/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的内容,这是一个参数,直到01阅读。 tempbuf看起来像'\ 0',所以对我来说是一个空字节。 但是,就我所知,所以我很感激任何帮助,因为我正在努力建立这个库。 预先感谢您。

回答

7

按照错误似乎在你的makefile-Werror=format-security标志是默认启用,这是非常好,不会引起字符串格式的任何安全问题就像printfscanf function.So为错误源处理的警告。所以如果你不担心安全问题,那就禁用它。或者,请在任何地方在代码中进行更改,如下所示,可能会删除错误。

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

为了安全使用snprintf

count += snprintf(tempbuf,buffer_length,"%s",format);/* printf it verbatim    */ 
+0

谢谢! “%s”修正了它。 – damian

相关问题