Changeset 1040 for cpp/frams/util


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

Location:
cpp/frams/util
Files:
2 edited

Legend:

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

    r996 r1040  
    44#include <assert.h>
    55#include <common/nonstd_math.h>
     6
     7#ifdef __ANDROID__
     8#include <android/log.h> //only needed to print error messages related to a workaround for Android bug
     9#endif
    610
    711
     
    273277                if (n < 0 && size >= (1 << 24)) //wants more than 16M
    274278                {
    275                         buf[size - 1] = 0; //just to ensure there is at least some ending \0 in memory... who knows what buggy vsnprintf() did.
    276                         __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);
     279                        p[size - 1] = 0; //just to ensure there is at least some ending \0 in memory... who knows what buggy vsnprintf() did.
     280                        __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);
    277281                        //in my tests, it always used 0 bytes, so it produced a 0-length string: ""
    278                         va_copy(ap_copy, ap);
    279                         n = vsprintf(buf, format, ap_copy); //hoping 16M is enough
    280                         va_end(ap_copy);
    281                         __android_log_print(ANDROID_LOG_INFO, LOG_APP_NAME, "Fallback to vsprintf() produced string: '%s'", buf);
     282                        va_start(ap, format);
     283                        n = vsnprintf(p, size, format, ap); //hoping 16M is enough
     284                        va_end(ap);
     285                        __android_log_print(ANDROID_LOG_INFO, LOG_APP_NAME, "Fallback to vsprintf() produced string: '%s'", p);
    282286                        if (n < 0) //vsprintf was also buggy. If we were strict, we should abort the app now.
    283287                        {
    284                                 strcpy(buf, "[STR_ERR] "); //a special prefix just to indicate the returned string is incorrect
    285                                 strcat(buf, format); //append and return the original formatting string
    286                                 __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "vsprintf() also failed, using the incorrect resulting string: '%s'", buf);
     288                                strcpy(p, "[STR_ERR] "); //a special prefix just to indicate the returned string is incorrect
     289                                strcat(p, format); //append and return the original formatting string
     290                                __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "vsprintf() also failed, using the incorrect resulting string: '%s'", p);
    287291                        }
    288                         n = strlen(buf); //pretend vsnprintf() or vsprintf() was OK to exit the endless loop
     292                        n = strlen(p); //pretend vsnprintf() or vsprintf() was OK to exit the endless loop
    289293        }
    290294#endif
  • 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.