From b135a210cc64e75a941e1e50b98e5f4460ccd1c8 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Tue, 10 Jun 2025 21:17:55 -0400 Subject: [PATCH] core: Avoid trivial snprintfs. --- src/httpd.js.c | 14 +++++++------- src/socket.js.c | 6 ++---- src/ssb.c | 18 +++++++++--------- src/ssb.connections.c | 16 ++++++++-------- src/ssb.db.c | 40 ++++++++++++++++++++-------------------- src/ssb.ebt.c | 8 ++++---- src/ssb.js.c | 8 ++++---- src/ssb.rpc.c | 20 ++++++++++---------- src/ssb.tests.c | 4 ++-- src/task.c | 8 ++++---- src/trace.c | 2 +- src/util.js.c | 17 ++++++++++++++++- src/util.js.h | 9 +++++++++ 13 files changed, 96 insertions(+), 74 deletions(-) diff --git a/src/httpd.js.c b/src/httpd.js.c index 7e7e74fd..ab2f5c25 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -931,7 +931,7 @@ static void _httpd_endpoint_app_blob_work(tf_ssb_t* ssb, void* user_data) char* app_path = tf_malloc(path_length); snprintf(app_path, path_length, "path:%s", data->user_app->app); const char* value = tf_ssb_db_get_property(ssb, data->user_app->user, app_path); - snprintf(data->app_blob_id, sizeof(data->app_blob_id), "%s", value); + tf_string_set(data->app_blob_id, sizeof(data->app_blob_id), value); tf_free(app_path); tf_free((void*)value); data->file = last_slash + 1; @@ -1130,7 +1130,7 @@ static void _httpd_endpoint_view_work(tf_ssb_t* ssb, void* user_data) char* app_path = tf_malloc(app_path_length); snprintf(app_path, app_path_length, "path:%s", user_app->app); const char* value = tf_ssb_db_get_property(ssb, user_app->user, app_path); - snprintf(blob_id, sizeof(blob_id), "%s", value); + tf_string_set(blob_id, sizeof(blob_id), value); tf_free(app_path); tf_free((void*)value); } @@ -1157,7 +1157,7 @@ static void _httpd_endpoint_view_work(tf_ssb_t* ssb, void* user_data) sqlite3* db = tf_ssb_acquire_db_writer(ssb); tf_ssb_db_add_blob_wants(db, blob_id); tf_ssb_release_db_writer(ssb, db); - snprintf(view->notify_want_blob_id, sizeof(view->notify_want_blob_id), "%s", blob_id); + tf_string_set(view->notify_want_blob_id, sizeof(view->notify_want_blob_id), blob_id); } } } @@ -1308,7 +1308,7 @@ static void _httpd_endpoint_save_work(tf_ssb_t* ssb, void* user_data) tf_ssb_db_set_property(ssb, user_app->user, app_path, blob_id)) { tf_ssb_db_add_value_to_array_property(ssb, user_app->user, "apps", user_app->app); - snprintf(save->blob_id, sizeof(save->blob_id), "%s", blob_id); + tf_string_set(save->blob_id, sizeof(save->blob_id), blob_id); save->response = 200; } else @@ -1341,7 +1341,7 @@ static void _httpd_endpoint_save_work(tf_ssb_t* ssb, void* user_data) char blob_id[k_blob_id_len] = { 0 }; if (tf_ssb_db_blob_store(ssb, request->body, request->content_length, blob_id, sizeof(blob_id), NULL)) { - snprintf(save->blob_id, sizeof(save->blob_id), "%s", blob_id); + tf_string_set(save->blob_id, sizeof(save->blob_id), blob_id); save->response = 200; } else @@ -1961,7 +1961,7 @@ static void _httpd_endpoint_login_work(tf_ssb_t* ssb, void* user_data) const char* return_url = _form_data_get(form_data, "return"); if (return_url) { - snprintf(login->location_header, sizeof(login->location_header), "%s", return_url); + tf_string_set(login->location_header, sizeof(login->location_header), return_url); } else { @@ -2066,7 +2066,7 @@ static void _httpd_endpoint_login_work(tf_ssb_t* ssb, void* user_data) const char* return_url = _form_data_get(form_data, "return"); if (return_url) { - snprintf(login->location_header, sizeof(login->location_header), "%s", return_url); + tf_string_set(login->location_header, sizeof(login->location_header), return_url); } else { diff --git a/src/socket.js.c b/src/socket.js.c index b295495e..7d577ba6 100644 --- a/src/socket.js.c +++ b/src/socket.js.c @@ -489,7 +489,7 @@ static JSValue _socket_connect(JSContext* context, JSValueConst this_val, int ar const char* node = JS_ToCString(context, argv[0]); const char* port = JS_ToCString(context, argv[1]); - snprintf(socket->_peerName, sizeof(socket->_peerName), "%s", node); + tf_string_set(socket->_peerName, sizeof(socket->_peerName), node); socket_resolve_data_t* data = tf_malloc(sizeof(socket_resolve_data_t)); memset(data, 0, sizeof(*data)); @@ -1062,9 +1062,7 @@ static void _socket_onShutdown(uv_shutdown_t* request, int status) } else { - char error[256]; - snprintf(error, sizeof(error), "uv_shutdown: %s", uv_strerror(status)); - tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error)); + tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "uv_shutdown: %s", uv_strerror(status))); } tf_free(request); } diff --git a/src/ssb.c b/src/ssb.c index 32c7ede2..308de4f4 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -668,7 +668,7 @@ void tf_ssb_connection_schedule_idle(tf_ssb_connection_t* connection, const char .callback = callback, .user_data = user_data, }; - snprintf(connection->scheduled[index].key, sizeof(connection->scheduled[index].key), "%s", key); + tf_string_set(connection->scheduled[index].key, sizeof(connection->scheduled[index].key), key); connection->scheduled_count++; uv_async_send(&connection->scheduled_async); @@ -789,7 +789,7 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ .dependent_connection = dependent_connection, .last_active = now_ms, }; - snprintf(request.name, sizeof(request.name), "%s", name); + tf_string_set(request.name, sizeof(request.name), name); int index = tf_util_insert_index(&request_number, connection->requests, connection->requests_count, sizeof(tf_ssb_request_t), _request_compare); connection->requests = tf_resize_vec(connection->requests, sizeof(tf_ssb_request_t) * (connection->requests_count + 1)); if (connection->requests_count - index) @@ -842,7 +842,7 @@ void tf_ssb_connection_add_new_message_request(tf_ssb_connection_t* connection, .request_number = request_number, .keys = keys, }; - snprintf(connection->message_requests[index].author, sizeof(connection->message_requests[index].author), "%s", author); + tf_string_set(connection->message_requests[index].author, sizeof(connection->message_requests[index].author), author); connection->message_requests_count++; } @@ -1703,7 +1703,7 @@ static void _tf_ssb_name_to_string(JSContext* context, JSValue object, char* buf else if (JS_IsString(name)) { const char* part_str = JS_ToCString(context, name); - snprintf(buffer, size, "%s", part_str); + tf_string_set(buffer, size, part_str); JS_FreeCString(context, part_str); } JS_FreeValue(context, name); @@ -1925,7 +1925,7 @@ JSValue tf_ssb_sign_message(tf_ssb_t* ssb, const char* author, const uint8_t* pr if (previous_id) { have_previous = *previous_id && previous_sequence > 0; - snprintf(actual_previous_id, sizeof(actual_previous_id), "%s", previous_id); + tf_string_set(actual_previous_id, sizeof(actual_previous_id), previous_id); actual_previous_sequence = previous_sequence; } else @@ -2965,7 +2965,7 @@ static tf_ssb_connection_t* _tf_ssb_connection_create( tf_ssb_connection_t* connection = _tf_ssb_connection_create_internal(ssb, "cli", s_connection_index++); connection->connect.data = connection; - snprintf(connection->host, sizeof(connection->host), "%s", host); + tf_string_set(connection->host, sizeof(connection->host), host); connection->port = ntohs(addr->sin_port); connection->connect_callback = callback; connection->connect_callback_user_data = user_data; @@ -3140,7 +3140,7 @@ void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* ke { tf_ssb_connections_store(ssb->connections_tracker, host, port, id); } - snprintf(connect->host, sizeof(connect->host), "%s", host); + tf_string_set(connect->host, sizeof(connect->host), host); memcpy(connect->key, key, k_id_bin_len); tf_ssb_ref(ssb); int r = uv_getaddrinfo(ssb->loop, &connect->req, _tf_on_connect_getaddrinfo, host, NULL, &(struct addrinfo) { .ai_family = AF_INET }); @@ -3189,7 +3189,7 @@ static void _tf_ssb_connect_with_invite( { tf_ssb_connections_store(ssb->connections_tracker, host, port, id); } - snprintf(connect->host, sizeof(connect->host), "%s", host); + tf_string_set(connect->host, sizeof(connect->host), host); memcpy(connect->key, key, k_id_bin_len); memcpy(connect->invite, invite, sizeof(connect->invite)); tf_ssb_ref(ssb); @@ -4436,7 +4436,7 @@ static void _tf_ssb_update_settings_after_work(tf_ssb_t* ssb, int result, void* uv_timer_start(&ssb->broadcast_timer, _tf_ssb_broadcast_timer, 2000, 2000); } ssb->discovery = update->discovery; - snprintf(ssb->seeds_host, sizeof(ssb->seeds_host), "%s", update->seeds_host); + tf_string_set(ssb->seeds_host, sizeof(ssb->seeds_host), update->seeds_host); _tf_ssb_start_update_settings(ssb); tf_free(update); } diff --git a/src/ssb.connections.c b/src/ssb.connections.c index cac231c1..83ce7c2a 100644 --- a/src/ssb.connections.c +++ b/src/ssb.connections.c @@ -61,9 +61,9 @@ static bool _tf_ssb_connections_get_next_connection(tf_ssb_connections_t* connec { if (sqlite3_bind_int(statement, 1, 60000) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) { - snprintf(host, host_size, "%s", sqlite3_column_text(statement, 0)); + tf_string_set(host, host_size, (const char*)sqlite3_column_text(statement, 0)); *port = sqlite3_column_int(statement, 1); - snprintf(key, key_size, "%s", sqlite3_column_text(statement, 2)); + tf_string_set(key, key_size, (const char*)sqlite3_column_text(statement, 2)); result = true; } sqlite3_finalize(statement); @@ -246,8 +246,8 @@ void tf_ssb_connections_store(tf_ssb_connections_t* connections, const char* hos *update = (tf_ssb_connections_update_t) { .port = port, }; - snprintf(update->host, sizeof(update->host), "%s", host); - snprintf(update->key, sizeof(update->key), "%s", key); + tf_string_set(update->host, sizeof(update->host), host); + tf_string_set(update->key, sizeof(update->key), key); _tf_ssb_connections_queue_update(connections, update); } @@ -258,8 +258,8 @@ void tf_ssb_connections_set_attempted(tf_ssb_connections_t* connections, const c .port = port, .attempted = true, }; - snprintf(update->host, sizeof(update->host), "%s", host); - snprintf(update->key, sizeof(update->key), "%s", key); + tf_string_set(update->host, sizeof(update->host), host); + tf_string_set(update->key, sizeof(update->key), key); _tf_ssb_connections_queue_update(connections, update); } @@ -270,8 +270,8 @@ void tf_ssb_connections_set_succeeded(tf_ssb_connections_t* connections, const c .port = port, .succeeded = true, }; - snprintf(update->host, sizeof(update->host), "%s", host); - snprintf(update->key, sizeof(update->key), "%s", key); + tf_string_set(update->host, sizeof(update->host), host); + tf_string_set(update->key, sizeof(update->key), key); _tf_ssb_connections_queue_update(connections, update); } diff --git a/src/ssb.db.c b/src/ssb.db.c index 8a683167..f761143e 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -789,10 +789,10 @@ void tf_ssb_db_store_message( .callback = callback, .user_data = user_data, }; - snprintf(store->id, sizeof(store->id), "%s", id); - snprintf(store->previous, sizeof(store->previous), "%s", previous ? previous : ""); - snprintf(store->author, sizeof(store->author), "%s", author); - snprintf(store->signature, sizeof(store->signature), "%s", signature); + tf_string_set(store->id, sizeof(store->id), id); + tf_string_set(store->previous, sizeof(store->previous), previous ? previous : ""); + tf_string_set(store->author, sizeof(store->author), author); + tf_string_set(store->signature, sizeof(store->signature), signature); JS_FreeCString(context, author); JS_FreeCString(context, previous); @@ -949,7 +949,7 @@ void tf_ssb_db_blob_get_async(tf_ssb_t* ssb, const char* id, tf_ssb_db_blob_get_ .callback = callback, .user_data = user_data, }; - snprintf(async->id, sizeof(async->id), "%s", id); + tf_string_set(async->id, sizeof(async->id), id); tf_ssb_run_work(ssb, _tf_ssb_db_blob_get_async_work, _tf_ssb_db_blob_get_async_after_work, async); } @@ -1046,7 +1046,7 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* if (result && out_id) { - snprintf(out_id, out_id_size, "%s", id); + tf_string_set(out_id, out_id_size, id); } if (out_new) { @@ -1068,7 +1068,7 @@ bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* aut { if (out_message_id) { - snprintf(out_message_id, out_message_id_size, "%s", (const char*)sqlite3_column_text(statement, 0)); + tf_string_set(out_message_id, out_message_id_size, (const char*)sqlite3_column_text(statement, 0)); } if (out_previous) { @@ -1081,7 +1081,7 @@ bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* aut } else { - snprintf(out_previous, out_previous_size, "%s", (const char*)sqlite3_column_text(statement, 1)); + tf_string_set(out_previous, out_previous_size, (const char*)sqlite3_column_text(statement, 1)); } } if (out_timestamp) @@ -1094,11 +1094,11 @@ bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* aut } if (out_hash) { - snprintf(out_hash, out_hash_size, "%s", (const char*)sqlite3_column_text(statement, 4)); + tf_string_set(out_hash, out_hash_size, (const char*)sqlite3_column_text(statement, 4)); } if (out_signature) { - snprintf(out_signature, out_signature_size, "%s", (const char*)sqlite3_column_text(statement, 5)); + tf_string_set(out_signature, out_signature_size, (const char*)sqlite3_column_text(statement, 5)); } if (out_flags) { @@ -1651,7 +1651,7 @@ static following_t* _make_following_node(const char* id, following_t*** followin (*following_count)++; memset(entry, 0, sizeof(*entry)); entry->depth = INT_MAX; - snprintf(entry->id, sizeof(entry->id), "%s", id); + tf_string_set(entry->id, sizeof(entry->id), id); } return entry; } @@ -1785,7 +1785,7 @@ tf_ssb_following_t* tf_ssb_db_following_deep(tf_ssb_t* ssb, const char** ids, in { if (following[i]->ref_count > 0 || include_blocks) { - snprintf(result[write_index].id, sizeof(result[write_index].id), "%s", following[i]->id); + tf_string_set(result[write_index].id, sizeof(result[write_index].id), following[i]->id); result[write_index].following_count = following[i]->following_count; result[write_index].blocking_count = following[i]->blocking_count; result[write_index].followed_by_count = following[i]->ref_count; @@ -1840,7 +1840,7 @@ const char** tf_ssb_db_following_deep_ids(tf_ssb_t* ssb, const char** ids, int c if (following[i]->ref_count > 0) { result[write_index] = result_ids + k_id_base64_len * write_index; - snprintf(result[write_index], k_id_base64_len, "%s", following[i]->id); + tf_string_set(result[write_index], k_id_base64_len, following[i]->id); write_index++; } } @@ -1939,8 +1939,8 @@ tf_ssb_db_stored_connection_t* tf_ssb_db_get_stored_connections(tf_ssb_t* ssb, i .last_attempt = sqlite3_column_int64(statement, 3), .last_success = sqlite3_column_int64(statement, 4), }; - snprintf(result[count].address, sizeof(result[count].address), "%s", (const char*)sqlite3_column_text(statement, 0)); - snprintf(result[count].pubkey, sizeof(result[count].pubkey), "%s", (const char*)sqlite3_column_text(statement, 2)); + tf_string_set(result[count].address, sizeof(result[count].address), (const char*)sqlite3_column_text(statement, 0)); + tf_string_set(result[count].pubkey, sizeof(result[count].pubkey), (const char*)sqlite3_column_text(statement, 2)); count++; } sqlite3_finalize(statement); @@ -1978,7 +1978,7 @@ bool tf_ssb_db_get_account_password_hash(tf_ssb_t* ssb, const char* name, char* { if (sqlite3_step(statement) == SQLITE_ROW) { - snprintf(out_password, password_size, "%s", (const char*)sqlite3_column_text(statement, 0)); + tf_string_set(out_password, password_size, (const char*)sqlite3_column_text(statement, 0)); result = true; } } @@ -2176,7 +2176,7 @@ bool tf_ssb_db_identity_get_active(sqlite3* db, const char* user, const char* pa if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, package_owner, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 3, package_name, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW) { - snprintf(out_identity, out_identity_size, "%s", (const char*)sqlite3_column_text(statement, 0)); + tf_string_set(out_identity, out_identity_size, (const char*)sqlite3_column_text(statement, 0)); found = true; } sqlite3_finalize(statement); @@ -2446,7 +2446,7 @@ bool tf_ssb_db_get_global_setting_string(sqlite3* db, const char* name, char* ou { if (sqlite3_step(statement) == SQLITE_ROW && sqlite3_column_type(statement, 0) != SQLITE_NULL) { - snprintf(out_value, size, "%s", sqlite3_column_text(statement, 0)); + tf_string_set(out_value, size, (const char*)sqlite3_column_text(statement, 0)); result = true; } } @@ -2458,7 +2458,7 @@ bool tf_ssb_db_get_global_setting_string(sqlite3* db, const char* name, char* ou } if (!result) { - snprintf(out_value, size, "%s", tf_util_get_default_global_setting_string(name)); + tf_string_set(out_value, size, tf_util_get_default_global_setting_string(name)); } return result; } @@ -2740,7 +2740,7 @@ tf_ssb_identity_info_t* tf_ssb_db_get_identity_info(tf_ssb_t* ssb, const char* u tf_ssb_db_identity_get_active(db, user, package_owner, package_name, info->active_identity, sizeof(info->active_identity)); if (!*info->active_identity && info->count) { - snprintf(info->active_identity, sizeof(info->active_identity), "%s", info->identity[0]); + tf_string_set(info->active_identity, sizeof(info->active_identity), info->identity[0]); } tf_ssb_release_db_reader(ssb, db); diff --git a/src/ssb.ebt.c b/src/ssb.ebt.c index 5a1434cf..8d37fe54 100644 --- a/src/ssb.ebt.c +++ b/src/ssb.ebt.c @@ -102,7 +102,7 @@ static ebt_entry_t* _ebt_get_entry(tf_ssb_ebt_t* ebt, const char* id) .in = -1, .out = -1, }; - snprintf(ebt->entries[index].id, sizeof(ebt->entries[index].id), "%s", id); + tf_string_set(ebt->entries[index].id, sizeof(ebt->entries[index].id), id); ebt->entries_count++; return &ebt->entries[index]; } @@ -212,7 +212,7 @@ static void _ebt_add_to_clock(ebt_get_clock_t* work, const char* id, int64_t val memmove(work->clock->entries + index + 1, work->clock->entries + index, (count - index) * sizeof(tf_ssb_ebt_clock_entry_t)); } work->clock->entries[index] = (tf_ssb_ebt_clock_entry_t) { .value = out_value }; - snprintf(work->clock->entries[index].id, sizeof(work->clock->entries[index].id), "%s", id); + tf_string_set(work->clock->entries[index].id, sizeof(work->clock->entries[index].id), id); work->clock->count = count + 1; } } @@ -279,7 +279,7 @@ static void _tf_ssb_ebt_get_send_clock_work(tf_ssb_connection_t* connection, voi { requested = tf_resize_vec(requested, (requested_count + 1) * sizeof(tf_ssb_ebt_clock_entry_t)); requested[requested_count] = (tf_ssb_ebt_clock_entry_t) { .value = -1 }; - snprintf(requested[requested_count].id, sizeof(requested[requested_count].id), "%s", entry->id); + tf_string_set(requested[requested_count].id, sizeof(requested[requested_count].id), entry->id); requested_count++; } } @@ -342,7 +342,7 @@ tf_ssb_ebt_clock_t* tf_ssb_ebt_get_messages_to_send(tf_ssb_ebt_t* ebt) { clock = tf_resize_vec(clock, sizeof(tf_ssb_ebt_clock_t) + (count + 1) * sizeof(tf_ssb_ebt_clock_entry_t)); clock->entries[count] = (tf_ssb_ebt_clock_entry_t) { .value = entry->in }; - snprintf(clock->entries[count].id, sizeof(clock->entries[count].id), "%s", entry->id); + tf_string_set(clock->entries[count].id, sizeof(clock->entries[count].id), entry->id); clock->count = ++count; } } diff --git a/src/ssb.js.c b/src/ssb.js.c index ac711965..e357247a 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -241,7 +241,7 @@ static JSValue _tf_ssb_deleteIdentity(JSContext* context, JSValueConst this_val, { delete_identity_t* work = tf_malloc(sizeof(delete_identity_t) + user_length + 1); *work = (delete_identity_t) { 0 }; - snprintf(work->id, sizeof(work->id), "%s", *id == '@' ? id + 1 : id); + tf_string_set(work->id, sizeof(work->id), *id == '@' ? id + 1 : id); memcpy(work->user, user, user_length + 1); result = JS_NewPromiseCapability(context, work->promise); tf_ssb_run_work(ssb, _tf_ssb_delete_identity_work, _tf_ssb_delete_identity_after_work, work); @@ -353,7 +353,7 @@ static JSValue _tf_ssb_swap_with_server_identity(JSContext* context, JSValueCons swap_with_server_identity_t* work = tf_malloc(sizeof(swap_with_server_identity_t) + user_length + 1); *work = (swap_with_server_identity_t) { 0 }; tf_ssb_whoami(ssb, work->server_id, sizeof(work->server_id)); - snprintf(work->id, sizeof(work->id), "%s", id); + tf_string_set(work->id, sizeof(work->id), id); memcpy(work->user, user, user_length + 1); result = JS_NewPromiseCapability(context, work->promise); tf_ssb_run_work(ssb, _tf_ssb_swap_with_server_identity_work, _tf_ssb_swap_with_server_identity_after_work, work); @@ -489,7 +489,7 @@ static JSValue _tf_ssb_getPrivateKey(JSContext* context, JSValueConst this_val, get_private_key_t* work = tf_malloc(sizeof(get_private_key_t) + user_length + 1); *work = (get_private_key_t) { .context = context }; memcpy(work->user, user, user_length + 1); - snprintf(work->id, sizeof(work->id), "%s", id); + tf_string_set(work->id, sizeof(work->id), id); JSValue result = JS_NewPromiseCapability(context, work->promise); tf_ssb_run_work(ssb, _tf_ssb_get_private_key_work, _tf_ssb_get_private_key_after_work, work); @@ -766,7 +766,7 @@ static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueCons append_message_t* work = tf_malloc(sizeof(append_message_t) + user_length + 1); *work = (append_message_t) { .context = context, .message = JS_DupValue(context, argv[2]) }; memcpy(work->user, user, user_length + 1); - snprintf(work->id, sizeof(work->id), "%s", id); + tf_string_set(work->id, sizeof(work->id), id); JS_FreeCString(context, id); JS_FreeCString(context, user); diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index 6caae886..eb1409f8 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -134,7 +134,7 @@ static void _tf_ssb_rpc_blobs_get(tf_ssb_connection_t* connection, uint8_t flags *work = (blobs_get_work_t) { .request_number = request_number, }; - snprintf(work->id, sizeof(work->id), "%s", id); + tf_string_set(work->id, sizeof(work->id), id); tf_ssb_connection_schedule_idle(connection, id, _tf_ssb_blobs_get_callback, work); JS_FreeCString(context, id); @@ -184,7 +184,7 @@ static void _tf_ssb_rpc_blobs_has(tf_ssb_connection_t* connection, uint8_t flags *work = (blobs_has_work_t) { .request_number = request_number, }; - snprintf(work->id, sizeof(work->id), "%s", id_str); + tf_string_set(work->id, sizeof(work->id), id_str); tf_ssb_connection_run_work(connection, _tf_ssb_rpc_blobs_has_work, _tf_ssb_rpc_blobs_has_after_work, work); JS_FreeCString(context, id_str); @@ -239,7 +239,7 @@ static void _tf_ssb_request_blob_wants_work(tf_ssb_connection_t* connection, voi { while (sqlite3_step(statement) == SQLITE_ROW) { - snprintf(work->out_id[work->out_id_count], sizeof(work->out_id[work->out_id_count]), "%s", (const char*)sqlite3_column_text(statement, 0)); + tf_string_set(work->out_id[work->out_id_count], sizeof(work->out_id[work->out_id_count]), (const char*)sqlite3_column_text(statement, 0)); work->out_id_count++; } } @@ -270,7 +270,7 @@ static void _tf_ssb_request_blob_wants_after_work(tf_ssb_connection_t* connectio } if (work->out_id_count) { - snprintf(blob_wants->last_id, sizeof(blob_wants->last_id), "%s", work->out_id[work->out_id_count - 1]); + tf_string_set(blob_wants->last_id, sizeof(blob_wants->last_id), work->out_id[work->out_id_count - 1]); } } tf_free(work); @@ -614,7 +614,7 @@ static void _tf_ssb_rpc_connection_blobs_get(tf_ssb_connection_t* connection, co { blobs_get_t* get = tf_malloc(sizeof(blobs_get_t) + size); *get = (blobs_get_t) { .ssb = tf_ssb_connection_get_ssb(connection), .connection = connection, .expected_size = size }; - snprintf(get->id, sizeof(get->id), "%s", blob_id); + tf_string_set(get->id, sizeof(get->id), blob_id); memset(get->buffer, 0, size); tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection); @@ -739,14 +739,14 @@ static void _tf_ssb_rpc_connection_blobs_createWants_callback( .connection = connection, .size = size, }; - snprintf(work->blob_id, sizeof(work->blob_id), "%s", blob_id); + tf_string_set(work->blob_id, sizeof(work->blob_id), blob_id); tf_ssb_connection_run_work(connection, _tf_ssb_rpc_connection_blobs_create_wants_work, _tf_ssb_rpc_connection_blobs_create_wants_after_work, work); } else { blob_get_t* get = tf_malloc(sizeof(blob_get_t)); *get = (blob_get_t) { .size = size }; - snprintf(get->id, sizeof(get->id), "%s", blob_id); + tf_string_set(get->id, sizeof(get->id), blob_id); tf_ssb_connection_schedule_idle(connection, blob_id, _tf_ssb_rpc_connection_blobs_get_idle, get); } JS_FreeCString(context, blob_id); @@ -1011,7 +1011,7 @@ static void _tf_ssb_connection_send_history_stream( .live = live, .end_request = end_request, }; - snprintf(async->author, sizeof(async->author), "%s", author); + tf_string_set(async->author, sizeof(async->author), author); tf_ssb_connection_schedule_idle(connection, author, _tf_ssb_connection_send_history_stream_callback, async); } } @@ -1340,7 +1340,7 @@ static void _tf_ssb_rpc_invite_use_callback( .ssb = ssb, .port = tf_ssb_connection_get_port(connection), }; - snprintf(work->host, sizeof(work->host), "%s", tf_ssb_connection_get_host(connection)); + tf_string_set(work->host, sizeof(work->host), tf_ssb_connection_get_host(connection)); tf_ssb_whoami(ssb, work->author, sizeof(work->author)); tf_ssb_get_private_key(ssb, work->private_key, sizeof(work->private_key)); tf_ssb_connection_get_id(connection, work->pub, sizeof(work->pub)); @@ -1878,7 +1878,7 @@ static void _tf_ssb_rpc_invite_use(tf_ssb_connection_t* connection, uint8_t flag JSValue feed = JS_GetPropertyStr(context, object, "feed"); tf_ssb_connection_get_id(connection, work->invite_public_key, sizeof(work->invite_public_key)); const char* id = JS_ToCString(context, feed); - snprintf(work->id, sizeof(work->id), "%s", id); + tf_string_set(work->id, sizeof(work->id), id); JS_FreeCString(context, id); JS_FreeValue(context, feed); JS_FreeValue(context, object); diff --git a/src/ssb.tests.c b/src/ssb.tests.c index ad8abe89..c0dc099b 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -811,7 +811,7 @@ static void _break_in_a_bit(tf_ssb_t* ssb, tf_ssb_connection_t* connection, cons .data = data, }, }; - snprintf(data->id, sizeof(data->id), "%s", id); + tf_string_set(data->id, sizeof(data->id), id); uv_timer_init(tf_ssb_get_loop(ssb), &data->timer); uv_timer_start(&data->timer, _close_callback, 3000, 0); } @@ -1154,7 +1154,7 @@ void tf_ssb_test_replicate(const tf_test_options_t* options) JSValue obj = JS_NewObject(context1); JS_SetPropertyStr(context1, obj, "type", JS_NewString(context1, "contact")); char self[k_id_base64_len]; - snprintf(self, sizeof(self), "%s", id1); + tf_string_set(self, sizeof(self), id1); char contact[k_id_base64_len]; snprintf(contact, sizeof(contact), "@%s", public[0]); JS_SetPropertyStr(context1, obj, "contact", JS_NewString(context1, contact)); diff --git a/src/task.c b/src/task.c index 0ea9e497..aa78a057 100644 --- a/src/task.c +++ b/src/task.c @@ -1166,7 +1166,7 @@ static JSValue _tf_task_executeSource(tf_task_t* task, const char* source, const JSValue result = JS_Eval(task->_context, source, strlen(source), name, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_ASYNC); if (!*task->_scriptName) { - snprintf(task->_scriptName, sizeof(task->_scriptName), "%s", name); + tf_string_set(task->_scriptName, sizeof(task->_scriptName), name); } tf_trace_end(task->_trace); return result; @@ -1966,7 +1966,7 @@ void tf_task_set_ssb_network_key(tf_task_t* task, const char* network_key) void tf_task_set_db_path(tf_task_t* task, const char* db_path) { - snprintf(task->_db_path, sizeof(task->_db_path), "%s", db_path); + tf_string_set(task->_db_path, sizeof(task->_db_path), db_path); } void tf_task_set_zip_path(tf_task_t* task, const char* zip_path) @@ -1976,7 +1976,7 @@ void tf_task_set_zip_path(tf_task_t* task, const char* zip_path) unzClose(task->_zip); task->_zip = NULL; } - snprintf(task->_zip_path, sizeof(task->_zip_path), "%s", zip_path); + tf_string_set(task->_zip_path, sizeof(task->_zip_path), zip_path); if (zip_path) { task->_zip = unzOpen(zip_path); @@ -1986,7 +1986,7 @@ void tf_task_set_zip_path(tf_task_t* task, const char* zip_path) void tf_task_set_root_path(tf_task_t* task, const char* path) { - snprintf(task->_root_path, sizeof(task->_root_path), "%s", path ? path : ""); + tf_string_set(task->_root_path, sizeof(task->_root_path), path ? path : ""); } const char* tf_task_get_zip_path(tf_task_t* task) diff --git a/src/trace.c b/src/trace.c index a26fbabe..d1b797ca 100644 --- a/src/trace.c +++ b/src/trace.c @@ -104,7 +104,7 @@ void tf_trace_destroy(tf_trace_t* trace) void tf_trace_set_process_name(tf_trace_t* trace, const char* name) { - snprintf(trace->process_name, sizeof(trace->process_name), "%s", name); + tf_string_set(trace->process_name, sizeof(trace->process_name), name); } void tf_trace_raw(tf_trace_t* trace, const char* buffer, size_t size) diff --git a/src/util.js.c b/src/util.js.c index fedb87b5..866613bb 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -612,7 +612,7 @@ static int _tf_util_backtrace_single_callback(void* data, uintptr_t pc, const ch { char** stack = data; char line[256]; - int length = snprintf(line, sizeof(line), "%s", function); + int length = (int)tf_string_set(line, sizeof(line), function); int current = *stack ? strlen(*stack) : 0; *stack = tf_resize_vec(*stack, current + length + 1); memcpy(*stack + current, line, length + 1); @@ -700,3 +700,18 @@ uint32_t tf_util_fnv32a(const void* buffer, int length, uint32_t start) } return result; } + +size_t tf_string_set(char* buffer, size_t size, const char* string) +{ + size_t length = string ? strlen(string) : 0; + length = tf_min(length, size - 1); + if (size) + { + if (length) + { + memcpy(buffer, string, length); + } + buffer[length] = 0; + } + return length; +} diff --git a/src/util.js.h b/src/util.js.h index 16e77acd..7a35bab5 100644 --- a/src/util.js.h +++ b/src/util.js.h @@ -233,4 +233,13 @@ bool tf_util_is_mobile(); */ uint32_t tf_util_fnv32a(const void* buffer, int length, uint32_t start); +/** +** Populate a string buffer, truncating if necessary. +** @param buffer The buffer. +** @param size The size of the buffer. +** @param string The value to set. +** @return The number of bytes set, not including the NULL terminator. +*/ +size_t tf_string_set(char* buffer, size_t size, const char* string); + /** @} */