| 24 | namespace base { |
| 25 | |
| 26 | void StringAppendV(std::string* dst, const char* format, va_list ap) { |
| 27 | // First try with a small fixed size buffer |
| 28 | char space[1024]; |
| 29 | |
| 30 | // It's possible for methods that use a va_list to invalidate |
| 31 | // the data in it upon use. The fix is to make a copy |
| 32 | // of the structure before using it and use that copy instead. |
| 33 | va_list backup_ap; |
| 34 | va_copy(backup_ap, ap); |
| 35 | int result = vsnprintf(space, sizeof(space), format, backup_ap); |
| 36 | va_end(backup_ap); |
| 37 | |
| 38 | if (result < static_cast<int>(sizeof(space))) { |
| 39 | if (result >= 0) { |
| 40 | // Normal case -- everything fit. |
| 41 | dst->append(space, result); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | if (result < 0) { |
| 46 | // Just an error. |
| 47 | return; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Increase the buffer size to the size requested by vsnprintf, |
| 52 | // plus one for the closing \0. |
| 53 | int length = result + 1; |
| 54 | char* buf = new char[length]; |
| 55 | |
| 56 | // Restore the va_list before we use it again |
| 57 | va_copy(backup_ap, ap); |
| 58 | result = vsnprintf(buf, length, format, backup_ap); |
| 59 | va_end(backup_ap); |
| 60 | |
| 61 | if (result >= 0 && result < length) { |
| 62 | // It fit |
| 63 | dst->append(buf, result); |
| 64 | } |
| 65 | delete[] buf; |
| 66 | } |
| 67 | |
| 68 | std::string StringPrintf(const char* fmt, ...) { |
| 69 | va_list ap; |
no outgoing calls
no test coverage detected