OLD | NEW |
1 // Copyright 2002 The RE2 Authors. All Rights Reserved. | 1 // Copyright 2002 The RE2 Authors. All Rights Reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 #include "util/util.h" | 5 #include "util/util.h" |
6 | 6 |
7 namespace re2 { | 7 namespace re2 { |
8 | 8 |
9 static void StringAppendV(string* dst, const char* format, va_list ap) { | 9 static void StringAppendV(string* dst, const char* format, va_list ap) { |
10 // First try with a small fixed size buffer | 10 // First try with a small fixed size buffer |
11 char space[1024]; | 11 char space[1024]; |
12 | 12 |
13 // It's possible for methods that use a va_list to invalidate | 13 // It's possible for methods that use a va_list to invalidate |
14 // the data in it upon use. The fix is to make a copy | 14 // the data in it upon use. The fix is to make a copy |
15 // of the structure before using it and use that copy instead. | 15 // of the structure before using it and use that copy instead. |
16 va_list backup_ap; | 16 va_list backup_ap; |
17 va_copy(backup_ap, ap); | 17 va_copy(backup_ap, ap); |
18 int result = vsnprintf(space, sizeof(space), format, backup_ap); | 18 int result = vsnprintf(space, sizeof(space), format, backup_ap); |
19 va_end(backup_ap); | 19 va_end(backup_ap); |
20 | 20 |
21 if ((result >= 0) && (static_cast<unsigned long>(result) < sizeof(space))) { | 21 if ((result >= 0) && (result < sizeof(space))) { |
22 // It fit | 22 // It fit |
23 dst->append(space, result); | 23 dst->append(space, result); |
24 return; | 24 return; |
25 } | 25 } |
26 | 26 |
27 // Repeatedly increase buffer size until it fits | 27 // Repeatedly increase buffer size until it fits |
28 int length = sizeof(space); | 28 int length = sizeof(space); |
29 while (true) { | 29 while (true) { |
30 if (result < 0) { | 30 if (result < 0) { |
31 // Older behavior: just try doubling the buffer size | 31 // Older behavior: just try doubling the buffer size |
32 length *= 2; | 32 length *= 2; |
33 } else { | 33 } else { |
34 // We need exactly "result+1" characters | 34 // We need exactly "result+1" characters |
35 length = result+1; | 35 length = result+1; |
36 } | 36 } |
37 char* buf = new char[length]; | 37 char* buf = new char[length]; |
38 | 38 |
39 // Restore the va_list before we use it again | 39 // Restore the va_list before we use it again |
40 va_copy(backup_ap, ap); | 40 va_copy(backup_ap, ap); |
41 #if !defined(_WIN32) | |
42 result = vsnprintf(buf, length, format, backup_ap); | 41 result = vsnprintf(buf, length, format, backup_ap); |
43 #else | |
44 // On Windows, the function takes five arguments, not four. With an array, | |
45 // the buffer size will be inferred, but not with a pointer. C'est la vie. | |
46 // (See https://github.com/google/re2/issues/40 for more details.) | |
47 result = vsnprintf(buf, length, _TRUNCATE, format, backup_ap); | |
48 #endif | |
49 va_end(backup_ap); | 42 va_end(backup_ap); |
50 | 43 |
51 if ((result >= 0) && (result < length)) { | 44 if ((result >= 0) && (result < length)) { |
52 // It fit | 45 // It fit |
53 dst->append(buf, result); | 46 dst->append(buf, result); |
54 delete[] buf; | 47 delete[] buf; |
55 return; | 48 return; |
56 } | 49 } |
57 delete[] buf; | 50 delete[] buf; |
58 } | 51 } |
(...skipping 17 matching lines...) Expand all Loading... |
76 } | 69 } |
77 | 70 |
78 void StringAppendF(string* dst, const char* format, ...) { | 71 void StringAppendF(string* dst, const char* format, ...) { |
79 va_list ap; | 72 va_list ap; |
80 va_start(ap, format); | 73 va_start(ap, format); |
81 StringAppendV(dst, format, ap); | 74 StringAppendV(dst, format, ap); |
82 va_end(ap); | 75 va_end(ap); |
83 } | 76 } |
84 | 77 |
85 } // namespace re2 | 78 } // namespace re2 |
OLD | NEW |