Ignore:
Timestamp:
11/30/20 04:32:21 (4 years ago)
Author:
Maciej Komosinski
Message:

Follow-up to r897: A workaround for Android bug in vsnprintf() and vsprintf() in SString, https://github.com/android-ndk/ndk/issues/879

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/sstring.cpp

    r996 r1040  
    417417                if (n < 0 && size >= (1 << 24)) //wants more than 16M
    418418                {
    419                         buf[size - 1] = 0; //just to ensure there is at least some ending \0 in memory... who knows what buggy vsnprintf() did.
    420                         __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "Giving up due to Android bug: vsnprintf() wants more than %d bytes, it used %zu bytes, for format='%s'", size, strlen(buf), format);
     419                        p[size - 1] = 0; //just to ensure there is at least some ending \0 in memory... who knows what buggy vsnprintf() did.
     420                        __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "Giving up due to Android bug: vsnprintf() wants more than %d bytes, it used %zu bytes, for format='%s'", size, strlen(p), format);
    421421                        //in my tests, it always used 0 bytes, so it produced a 0-length string: ""
    422                         va_copy(ap_copy, ap);
    423                         n = vsprintf(buf, format, ap_copy); //hoping 16M is enough
    424                         va_end(ap_copy);
    425                         __android_log_print(ANDROID_LOG_INFO, LOG_APP_NAME, "Fallback to vsprintf() produced string: '%s'", buf);
     422                        va_start(ap, format);
     423                        n = vsnprintf(p, size, format, ap); //hoping 16M is enough
     424                        va_end(ap);
     425                        __android_log_print(ANDROID_LOG_INFO, LOG_APP_NAME, "Fallback to vsprintf() produced string: '%s'", p);
    426426                        if (n < 0) //vsprintf was also buggy. If we were strict, we should abort the app now.
    427427                        {
    428                                 strcpy(buf, "[STR_ERR] "); //a special prefix just to indicate the returned string is incorrect
    429                                 strcat(buf, format); //append and return the original formatting string
    430                                 __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "vsprintf() also failed, using the incorrect resulting string: '%s'", buf);
     428                                strcpy(p, "[STR_ERR] "); //a special prefix just to indicate the returned string is incorrect
     429                                strcat(p, format); //append and return the original formatting string
     430                                __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "vsprintf() also failed, using the incorrect resulting string: '%s'", p);
    431431                        }
    432                         n = strlen(buf); //pretend vsnprintf() or vsprintf() was OK to exit the endless loop
     432                        n = strlen(p); //pretend vsnprintf() or vsprintf() was OK to exit the endless loop
    433433                }
    434434#endif
Note: See TracChangeset for help on using the changeset viewer.