source: cpp/common/util-string.cpp @ 892

Last change on this file since 892 was 892, checked in by Maciej Komosinski, 3 weeks ago

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

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "util-string.h"
6#include <stdarg.h>
7#include "nonstd_stdio.h"
8#include "nonstd.h"
9#include <assert.h>
10#include <cstdlib> //malloc()
11#ifdef USE_VIRTFILE
12#include <common/virtfile/virtfile.h>
13#endif
14#ifdef __ANDROID__
15#include <android/log.h> //only needed to print error messages related to a workaround for Android bug
16#endif
17
18string ssprintf_va(const char* format, va_list ap)
19{
20        string s; //clang crashed when this declaration was in s=buf
21        int size = 256;
22        char* buf;
23        va_list ap_copy; // "va_list ap" can only by used once by printf-type functions as they advance the current argument pointer (crashed on linux x86_64)
24        // (does not apply to SString::sprintf, it does not have the va_list variant)
25
26        //almost like SString::sprintf, but there is no common code to share because SString can use its directWrite to avoid double allocating/copying
27#ifdef USE_VSCPRINTF
28        va_copy(ap_copy, ap);
29        size = _vscprintf(format, ap_copy) + 1; //+1 for terminating null character
30        va_end(ap_copy);
31#endif
32
33        while (1)
34        {
35                buf = (char*)malloc(size);
36                assert(buf != NULL);
37                va_copy(ap_copy, ap);
38                int n = vsnprintf(buf, size, format, ap_copy);
39                va_end(ap_copy);
40
41#ifdef __ANDROID__
42                //Workaround for Android bug. /system/lib64/libc.so? maybe only arm 64-bit? "If an encoding error occurs, a negative number is returned". On some devices keeps returning -1 forever.
43                //https://github.com/android-ndk/ndk/issues/879 but unfortunately during google play tests (Firebase Test Lab) this problem turned out to be not limited to Chinese devices and occurred in Mate 9, Galaxy S9, Pixel, Pixel 2, Moto Z (even with the en_GB locale; the locale is not important but the problem seem to be utf8 non-ascii chars in the format string).
44                if (n < 0 && size >= (1 << 24)) //wants more than 16M
45                {
46                        buf[size - 1] = 0; //just to ensure there is at least some ending \0 in memory... who knows what buggy vsnprintf() did.
47                        __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);
48                        //in my tests, it always used 0 bytes, so it produced a 0-length string: ""
49                        va_copy(ap_copy, ap);
50                        n = vsprintf(buf, format, ap_copy); //hoping 16M is enough
51                        va_end(ap_copy);
52                        __android_log_print(ANDROID_LOG_INFO, LOG_APP_NAME, "Fallback to vsprintf() produced string: '%s'", buf);
53                        if (n < 0) //vsprintf was also buggy. If we were strict, we should abort the app now.
54                        {
55                                strcpy(buf, "[STR_ERR] "); //just to indicate the returned string is invalid
56                                strcat(buf, format); //return just the original formatting string
57                                __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "vsprintf() also failed, returning incorrect string '%s'", buf);
58                        }
59                        n = 0; //pretend vsnprintf() or vsprintf() was OK to exit the endless loop
60                }
61#endif
62
63                if (n > -1 && n < size)
64                {
65                        s = buf;
66                        free(buf);
67                        return s;
68                }
69#ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE
70                if (n > -1)    /* glibc 2.1 */
71                        size = n + 1; /* precisely what is needed */
72                else           /* glibc 2.0 */
73#endif
74                        size *= 2;  /* twice the old size */
75                free(buf);
76        }
77}
78
79char* strmove(char *a, char *b) //strcpy that works well for overlapping strings ("Source and destination overlap")
80{
81        if (a == NULL || b == NULL)
82                return NULL;
83        memmove(a, b, strlen(b) + 1);
84        return a;
85}
86
87string ssprintf(const char* format, ...)
88{
89        va_list ap;
90        va_start(ap, format);
91        string ret = ssprintf_va(format, ap); //is it too wasteful? copying the string again... unless the compiler can handle it better
92        va_end(ap);
93        return ret;
94}
95
96string stripExt(const string& filename)
97{
98        size_t dot = filename.rfind('.');
99        if (dot == string::npos) return filename;
100        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
101        if ((sep == string::npos) || (sep < dot))
102                return filename.substr(0, dot);
103        return filename;
104}
105
106string stripFileDir(const string& filename)
107{
108        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
109        if (sep == string::npos) return filename;
110        return filename.substr(sep + 1);
111}
112
113string getFileExt(const string& filename)
114{
115        size_t dot = filename.rfind('.');
116        if (dot == string::npos) return string("");
117        size_t sep = filename.rfind(PATH_SEPARATOR_CHAR);
118        if ((sep == string::npos) || (sep < dot))
119                return filename.substr(dot);
120        return string("");
121}
122
123string getFileDir(const string& filename)
124{
125        size_t slash = filename.rfind(PATH_SEPARATOR_CHAR);
126        if (slash == string::npos) return string("");
127        return filename.substr(0, slash);
128}
Note: See TracBrowser for help on using the repository browser.