| 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) && (result < sizeof(space))) { | 21 if ((result >= 0) && (static_cast<unsigned long>(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) |
| 41 result = vsnprintf(buf, length, format, backup_ap); | 42 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 |
| 42 va_end(backup_ap); | 49 va_end(backup_ap); |
| 43 | 50 |
| 44 if ((result >= 0) && (result < length)) { | 51 if ((result >= 0) && (result < length)) { |
| 45 // It fit | 52 // It fit |
| 46 dst->append(buf, result); | 53 dst->append(buf, result); |
| 47 delete[] buf; | 54 delete[] buf; |
| 48 return; | 55 return; |
| 49 } | 56 } |
| 50 delete[] buf; | 57 delete[] buf; |
| 51 } | 58 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 69 } | 76 } |
| 70 | 77 |
| 71 void StringAppendF(string* dst, const char* format, ...) { | 78 void StringAppendF(string* dst, const char* format, ...) { |
| 72 va_list ap; | 79 va_list ap; |
| 73 va_start(ap, format); | 80 va_start(ap, format); |
| 74 StringAppendV(dst, format, ap); | 81 StringAppendV(dst, format, ap); |
| 75 va_end(ap); | 82 va_end(ap); |
| 76 } | 83 } |
| 77 | 84 |
| 78 } // namespace re2 | 85 } // namespace re2 |
| OLD | NEW |