blob: 27e81afd9f26c0bb131f6b7319a5303898c2f0ba [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2From: Peter Johnson <johnson.peter@gmail.com>
3Date: Mon, 9 Oct 2023 19:28:08 -0700
4Subject: [PATCH 11/11] Avoid use of sprintf
5
6---
7 src/google/protobuf/stubs/strutil.cc | 14 +++++++++++---
8 1 file changed, 11 insertions(+), 3 deletions(-)
9
10diff --git a/src/google/protobuf/stubs/strutil.cc b/src/google/protobuf/stubs/strutil.cc
11index 3462e91ff273dc071628f06b91698a0f166514fc..e063d0d3039c87d55a6df0dbfe50f365184fe292 100644
12--- a/src/google/protobuf/stubs/strutil.cc
13+++ b/src/google/protobuf/stubs/strutil.cc
14@@ -503,10 +503,18 @@ int CEscapeInternal(const char* src, int src_len, char* dest,
15 (last_hex_escape && isxdigit(*src)))) {
16 if (dest_len - used < 4) // need space for 4 letter escape
17 return -1;
18- sprintf(dest + used, (use_hex ? "\\x%02x" : "\\%03o"),
19- static_cast<uint8_t>(*src));
20+ dest[used++] = '\\';
21+ if (use_hex) {
22+ constexpr char hexdigits[] = "0123456789abcdef";
23+ dest[used++] = 'x';
24+ dest[used++] = hexdigits[(static_cast<uint8_t>(*src) >> 4) & 0xf];
25+ dest[used++] = hexdigits[static_cast<uint8_t>(*src) & 0xf];
26+ } else {
27+ dest[used++] = '0' + ((static_cast<uint8_t>(*src) >> 6) & 0x3);
28+ dest[used++] = '0' + ((static_cast<uint8_t>(*src) >> 3) & 0x7);
29+ dest[used++] = '0' + (static_cast<uint8_t>(*src) & 0x7);
30+ }
31 is_hex_escape = use_hex;
32- used += 4;
33 } else {
34 dest[used++] = *src; break;
35 }