WillAyd commented on code in PR #587:
URL: https://github.com/apache/arrow-adbc/pull/587#discussion_r1167294994


##########
c/driver/common/utils.c:
##########
@@ -124,19 +124,26 @@ void StringBuilderInit(struct StringBuilder* builder, 
size_t initial_size) {
   builder->size = 0;
   builder->capacity = initial_size;
 }
-void StringBuilderAppend(struct StringBuilder* builder, const char* value) {
-  size_t length = strlen(value);
-  size_t new_size = builder->size + length;
-  if (new_size > builder->capacity) {
-    size_t new_capacity = builder->size + length - builder->capacity;
-    if (builder->size == 0) new_capacity++;
-
-    builder->buffer = realloc(builder->buffer, new_capacity);
-    builder->capacity = new_capacity;
+void StringBuilderAppend(struct StringBuilder* builder, const char* fmt, ...) {
+  va_list argptr;
+  size_t bytes_available = builder->capacity - builder->size;
+
+  va_start(argptr, fmt);
+  int n = vsnprintf(builder->buffer + builder->size, bytes_available, fmt, 
argptr);
+  va_end(argptr);
+
+  if (n < 0) {                        // TODO: handle error
+  } else if (n >= bytes_available) {  // output was truncated
+    builder->buffer = (char*)realloc(builder->buffer, builder->capacity + n + 
1);

Review Comment:
   Might be misreading the current implementation but I think it still stores 
the null terminator, just doesn't include it as part of size. Doing the same 
here, hence the `n + 1` in allocation / capacity expressions but just `+= n` 
for the size



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to