1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #include "upb/reflection/internal/strdup2.h" 9 10 #include <string.h> 11 12 #include "upb/mem/arena.h" 13 14 // Must be last. 15 #include "upb/port/def.inc" 16 upb_strdup2(const char * s,size_t len,upb_Arena * a)17char* upb_strdup2(const char* s, size_t len, upb_Arena* a) { 18 size_t n; 19 char* p; 20 21 // Prevent overflow errors. 22 if (len == SIZE_MAX) return NULL; 23 24 // Always null-terminate, even if binary data; but don't rely on the input to 25 // have a null-terminating byte since it may be a raw binary buffer. 26 n = len + 1; 27 p = upb_Arena_Malloc(a, n); 28 if (p) { 29 if (len != 0) memcpy(p, s, len); 30 p[len] = 0; 31 } 32 return p; 33 } 34