Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #44 from ali-ince/1.7-fix-va-list-usage
Browse files Browse the repository at this point in the history
Avoid multiple use of va_list
  • Loading branch information
ali-ince authored Sep 20, 2018
2 parents 0d4f311 + 0f3deb2 commit 4834786
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
18 changes: 10 additions & 8 deletions seabolt/src/bolt/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ void BoltLog_destroy(struct BoltLog* log)

void _perform_log_call(log_func func, int state, const char* format, va_list args)
{
int size = 512*sizeof(char);
size_t size = 512*sizeof(char);
char* message_fmt = (char*) BoltMem_allocate(size);
while (1) {
int written = vsnprintf(message_fmt, size, format, args);
va_list args_copy;
va_copy(args_copy, args);
size_t written = vsnprintf(message_fmt, size, format, args_copy);
va_end(args_copy);
if (written<size) {
break;
}
BoltMem_deallocate(message_fmt, size);
size = size*2;
message_fmt = (char*) BoltMem_allocate(size);

message_fmt = (char*) BoltMem_reallocate(message_fmt, size, written+1);
size = written+1;
}

func(state, message_fmt);
Expand Down Expand Up @@ -122,15 +125,14 @@ void BoltLog_message(const struct BoltLog* log, const char* peer, bolt_request r
struct BoltValue* fields, name_resolver_func struct_name_resolver, name_resolver_func message_name_resolver)
{
if (log!=NULL && log->debug_enabled) {
const char* message_name = NULL;
const char* message_name = "?";
if (message_name_resolver!=NULL) {
message_name = message_name_resolver(code);
}

struct StringBuilder* builder = StringBuilder_create();
BoltValue_write(builder, fields, struct_name_resolver);
BoltLog_debug(log, "%s[%" PRIu64 "] %s %s", peer, request_id, message_name==NULL ? "?" : message_name,
StringBuilder_get_string(builder));
BoltLog_debug(log, "%s[%" PRIu64 "] %s %s", peer, request_id, message_name, StringBuilder_get_string(builder));
StringBuilder_destroy(builder);
}
}
24 changes: 12 additions & 12 deletions seabolt/src/bolt/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
struct StringBuilder* StringBuilder_create()
{
struct StringBuilder* builder = (struct StringBuilder*) BoltMem_allocate(sizeof(struct StringBuilder));
builder->buffer = BoltMem_allocate(1*sizeof(char));
builder->buffer[0] = '\0';
builder->buffer = (char*) BoltMem_allocate(256*sizeof(char));
builder->buffer[0] = 0;
builder->buffer_pos = 0;
builder->buffer_size = 1;
builder->buffer_size = 256;
return builder;
}

Expand All @@ -45,7 +45,7 @@ void StringBuilder_ensure_buffer(struct StringBuilder* builder, size_t size_to_a
return;
}

size_t new_size = builder->buffer_pos+size_to_add+1;
size_t new_size = builder->buffer_pos+size_to_add;
builder->buffer = (char*) BoltMem_reallocate(builder->buffer, builder->buffer_size, new_size);
builder->buffer_size = new_size;
}
Expand All @@ -57,27 +57,27 @@ void StringBuilder_append(struct StringBuilder* builder, const char* string)

void StringBuilder_append_n(struct StringBuilder* builder, const char* string, const size_t len)
{
StringBuilder_ensure_buffer(builder, len);
StringBuilder_ensure_buffer(builder, len+1);
strncpy(builder->buffer+builder->buffer_pos, string, len);
builder->buffer_pos += len;
builder->buffer[builder->buffer_pos] = '\0';
builder->buffer[builder->buffer_pos] = 0;
}

void StringBuilder_append_f(struct StringBuilder* builder, const char* format, ...)
{
int size = 512*sizeof(char);
char* message_fmt = BoltMem_allocate(size);
size_t size = 10240*sizeof(char);
char* message_fmt = (char*) BoltMem_allocate(size);
while (1) {
va_list args;
va_start(args, format);
int written = vsnprintf(message_fmt, size, format, args);
size_t written = vsnprintf(message_fmt, size, format, args);
va_end(args);
if (written<size) {
break;
}
BoltMem_deallocate(message_fmt, size);
size = size*2;
message_fmt = BoltMem_allocate(size);

message_fmt = (char*) BoltMem_reallocate(message_fmt, size, written+1);
size = written+1;
}

StringBuilder_append(builder, message_fmt);
Expand Down
4 changes: 2 additions & 2 deletions seabolt/src/bolt/values.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,10 @@ int BoltValue_write(struct StringBuilder* builder, struct BoltValue* value, name
StringBuilder_append(builder, "{");
int comma = 0;
for (int i = 0; i<value->size; i++) {
const char* key = BoltDictionary_get_key(value, i);
struct BoltValue* key = BoltDictionary_key(value, i);
if (key!=NULL) {
if (comma) StringBuilder_append(builder, ", ");
StringBuilder_append_n(builder, key, (size_t) (BoltDictionary_get_key_size(value, i)));
StringBuilder_append_n(builder, BoltString_get(key), (size_t) key->size);
StringBuilder_append(builder, ": ");
BoltValue_write(builder, BoltDictionary_value(value, i), struct_name_resolver);
comma = 1;
Expand Down

0 comments on commit 4834786

Please sign in to comment.