diff --git a/conf_cli.c b/conf_cli.c index 6320bda6..753bb70f 100644 --- a/conf_cli.c +++ b/conf_cli.c @@ -253,7 +253,7 @@ static int app_config_paths(const struct cli_parsed *parsed, struct cli_context cli_field_name(context, "SERVAL_CACHE_PATH", ":"); cli_put_string(context, path, "\n"); } - strbuf sb = strbuf_local(path, sizeof path); + strbuf sb = strbuf_local_buf(path); strbuf_system_log_path(sb); if (!strbuf_overrun(sb)) { cli_field_name(context, "SYSTEM_LOG_PATH", ":"); diff --git a/dna_helper.c b/dna_helper.c index ac0129b7..ee9cafe0 100644 --- a/dna_helper.c +++ b/dna_helper.c @@ -598,7 +598,7 @@ dna_helper_enqueue(struct subscriber *source, mdp_port_t source_port, const char return 0; } char buffer[sizeof request_buffer]; - strbuf b = strbuf_local(request_bufptr == request_buffer ? buffer : request_buffer, sizeof buffer); + strbuf b = request_bufptr == request_buffer ? strbuf_local_buf(buffer) : strbuf_local_buf(request_buffer); strbuf_tohex(b, SID_STRLEN, source->sid.binary); strbuf_putc(b, '|'); strbuf_puts(b, did); diff --git a/instance.c b/instance.c index 38e27727..2e2569dc 100644 --- a/instance.c +++ b/instance.c @@ -206,7 +206,7 @@ int create_serval_instance_dir() char path[PATH_MAX]; // emkdire_info can log if paths don't exist, which will also try to create paths... // so try to create logging folders first - strbuf sb = strbuf_local(path, sizeof path); + strbuf sb = strbuf_local_buf(path); strbuf_system_log_path(sb); if (!strbuf_overrun(sb) && emkdirs_info(path, 0700) == -1) ret = -1; diff --git a/log.c b/log.c index d284bda2..76877f7e 100644 --- a/log.c +++ b/log.c @@ -450,7 +450,7 @@ static void _open_log_file(_log_iterator *it) if (_log_file_path == NULL) _log_file_path = getenv("SERVALD_LOG_FILE"); if (_log_file_path == NULL && !cf_limbo) { - strbuf sbfile = strbuf_local(_log_file_path_buf, sizeof _log_file_path_buf); + strbuf sbfile = strbuf_local_buf(_log_file_path_buf); strbuf_serval_log_path(sbfile); strbuf_path_join(sbfile, config.log.file.directory_path, "", NULL); // with trailing '/' _compute_file_start_time(it); diff --git a/log_util.c b/log_util.c index bdec9794..df96be7d 100644 --- a/log_util.c +++ b/log_util.c @@ -37,7 +37,7 @@ int logDump(int level, struct __sourceloc whence, char *name, const unsigned cha if (name) logMessage(level, whence, "Dump of %s", name); for(i = 0; i < len; i += 16) { - strbuf b = strbuf_local(buf, sizeof buf); + strbuf b = strbuf_local_buf(buf); strbuf_sprintf(b, " %04zx :", i); int j; for (j = 0; j < 16 && i + j < len; j++) @@ -208,7 +208,7 @@ int logBacktrace(int level, struct __sourceloc whence) int status = 0; if (waitpid(child_pid, &status, 0) == -1) WHY_perror("waitpid"); - strbuf b = strbuf_local(buf, sizeof buf); + strbuf b = strbuf_local_buf(buf); strbuf_append_exit_status(b, status); logMessage(level, __NOWHERE__, "gdb %s", buf); unlink(tempfile); diff --git a/network_cli.c b/network_cli.c index 624e6b73..d33f150a 100644 --- a/network_cli.c +++ b/network_cli.c @@ -470,7 +470,7 @@ static int app_route_print(const struct cli_parsed *parsed, struct cli_context * cli_put_string(context, alloca_tohex_sid_t(p->sid), ":"); char flags[32]; - strbuf b = strbuf_local(flags, sizeof flags); + strbuf b = strbuf_local_buf(flags); switch (p->reachable){ case REACHABLE_SELF: diff --git a/overlay_interface.c b/overlay_interface.c index 84c2f3be..a467ca20 100644 --- a/overlay_interface.c +++ b/overlay_interface.c @@ -912,7 +912,7 @@ static int send_local_packet(int fd, const uint8_t *bytes, size_t len, const cha { struct socket_address addr; - strbuf d = strbuf_local(addr.local.sun_path, sizeof addr.local.sun_path); + strbuf d = strbuf_local_buf(addr.local.sun_path); strbuf_path_join(d, folder, file, NULL); if (strbuf_overrun(d)) return WHYF("interface file name overrun: %s", alloca_str_toprint(strbuf_str(d))); diff --git a/overlay_mdp.c b/overlay_mdp.c index 2cf6e7cd..5ed3fa02 100644 --- a/overlay_mdp.c +++ b/overlay_mdp.c @@ -698,7 +698,7 @@ int overlay_mdp_dnalookup_reply(struct subscriber *dest, mdp_port_t dest_port, /* build reply as TOKEN|URI|DID|NAME| */ char buff[256]; - strbuf b = strbuf_local(buff, sizeof buff); + strbuf b = strbuf_local_buf(buff); strbuf_tohex(b, SID_STRLEN, resolved_sid->sid.binary); strbuf_sprintf(b, "|%s|%s|%s|", uri, did, name?name:""); if (strbuf_overrun(b)) diff --git a/rhizome_bundle.c b/rhizome_bundle.c index 602edf13..7484c612 100644 --- a/rhizome_bundle.c +++ b/rhizome_bundle.c @@ -1325,7 +1325,7 @@ void _rhizome_manifest_free(struct __sourceloc __whence, rhizome_manifest *m) static int rhizome_manifest_pack_variables(rhizome_manifest *m) { assert(m->var_count <= NELS(m->vars)); - strbuf sb = strbuf_local((char*)m->manifestdata, sizeof m->manifestdata); + strbuf sb = strbuf_local_buf(m->manifestdata); unsigned i; for (i = 0; i < m->var_count; ++i) { strbuf_puts(sb, m->vars[i]); diff --git a/rhizome_database.c b/rhizome_database.c index 2074b885..30228096 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -318,7 +318,7 @@ int rhizome_opendb() All changes should attempt to preserve all existing interesting data */ char buf[UUID_STRLEN + 1]; - int r = sqlite_exec_strbuf_retry(&retry, strbuf_local(buf, sizeof buf), "SELECT uuid from IDENTITY LIMIT 1;", END); + int r = sqlite_exec_strbuf_retry(&retry, strbuf_local_buf(buf), "SELECT uuid from IDENTITY LIMIT 1;", END); if (r == -1) RETURN(-1); if (r) { @@ -1579,7 +1579,7 @@ enum rhizome_bundle_status rhizome_find_duplicate(const rhizome_manifest *m, rhi if (m->service == NULL) return WHY("Manifest has no service"); char sqlcmd[1024]; - strbuf b = strbuf_local(sqlcmd, sizeof sqlcmd); + strbuf b = strbuf_local_buf(sqlcmd); strbuf_puts(b, "SELECT id, manifest, author FROM manifests WHERE filesize = ? AND service = ?"); assert(m->filesize != RHIZOME_SIZE_UNSET); if (m->filesize > 0) diff --git a/rhizome_direct.c b/rhizome_direct.c index 336a5813..ea3bc6eb 100644 --- a/rhizome_direct.c +++ b/rhizome_direct.c @@ -488,7 +488,7 @@ static int rhizome_sync_with_peers(int mode, int peer_count, const struct config const struct config_rhizome_peer *peer = peers[peer_number]; if (strcasecmp(peer->protocol, "http") != 0) return WHYF("Unsupported Rhizome Direct protocol %s", alloca_str_toprint(peer->protocol)); - strbuf h = strbuf_local(state->host, sizeof state->host); + strbuf h = strbuf_local_buf(state->host); strbuf_puts(h, peer->host); if (strbuf_overrun(h)) return WHYF("Rhizome Direct host name too long: %s", alloca_str_toprint(peer->host)); diff --git a/rhizome_direct_http.c b/rhizome_direct_http.c index 8fd6510f..f14669aa 100644 --- a/rhizome_direct_http.c +++ b/rhizome_direct_http.c @@ -503,7 +503,7 @@ void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r) char boundary[20]; char buffer[8192]; - strbuf bb = strbuf_local(boundary, sizeof boundary); + strbuf bb = strbuf_local_buf(boundary); strbuf_sprintf(bb, "%08lx%08lx", random(), random()); assert(!strbuf_overrun(bb)); strbuf content_preamble = strbuf_alloca(200); @@ -522,7 +522,7 @@ void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r) + r->cursor->buffer_offset_bytes + r->cursor->buffer_used + strbuf_len(content_postamble); - strbuf request = strbuf_local(buffer, sizeof buffer); + strbuf request = strbuf_local_buf(buffer); strbuf_sprintf(request, "POST /rhizome/enquiry HTTP/1.0\r\n" "Content-Length: %d\r\n" diff --git a/rhizome_fetch.c b/rhizome_fetch.c index 2548c1d1..09fbff38 100644 --- a/rhizome_fetch.c +++ b/rhizome_fetch.c @@ -511,7 +511,7 @@ schedule_fetch(struct rhizome_fetch_slot *slot) slot->manifest->dataFileName = NULL; slot->manifest->dataFileUnlinkOnFree = 0; - strbuf r = strbuf_local(slot->request, sizeof slot->request); + strbuf r = strbuf_local_buf(slot->request); strbuf_sprintf(r, "GET /rhizome/file/%s HTTP/1.0\r\n", alloca_tohex_rhizome_filehash_t(slot->manifest->filehash)); if (slot->manifest->is_journal){ @@ -569,7 +569,7 @@ schedule_fetch(struct rhizome_fetch_slot *slot) status_ok: ; } else { - strbuf r = strbuf_local(slot->request, sizeof slot->request); + strbuf r = strbuf_local_buf(slot->request); strbuf_sprintf(r, "GET /rhizome/manifestbyprefix/%s HTTP/1.0\r\n\r\n", alloca_tohex(slot->bid.binary, slot->prefix_length)); if (strbuf_overrun(r)) RETURN(WHY("request overrun")); diff --git a/rhizome_http.c b/rhizome_http.c index 066e5d7f..f08f8b45 100644 --- a/rhizome_http.c +++ b/rhizome_http.c @@ -83,7 +83,7 @@ static int rhizome_status_page(httpd_request *r, const char *remainder) if (r->http.verb != HTTP_VERB_GET) return 405; char buf[32*1024]; - strbuf b = strbuf_local(buf, sizeof buf); + strbuf b = strbuf_local_buf(buf); strbuf_puts(b, ""); strbuf_sprintf(b, "%d HTTP requests
", current_httpd_request_count); strbuf_sprintf(b, "%d Bundles transferring via MDP
", rhizome_cache_count()); diff --git a/server_httpd.c b/server_httpd.c index 095f022a..eb7e0945 100644 --- a/server_httpd.c +++ b/server_httpd.c @@ -21,7 +21,7 @@ static int root_page(httpd_request *r, const char *remainder) if (r->http.verb != HTTP_VERB_GET) return 405; char temp[8192]; - strbuf b = strbuf_local(temp, sizeof temp); + strbuf b = strbuf_local_buf(temp); strbuf_sprintf(b, "" "

Hello, I'm %s*

", alloca_tohex_sid_t_trunc(my_subscriber->sid, 16)); @@ -64,7 +64,7 @@ static int neighbour_page(httpd_request *r, const char *remainder) if (r->http.verb != HTTP_VERB_GET) return 405; char buf[8*1024]; - strbuf b = strbuf_local(buf, sizeof buf); + strbuf b = strbuf_local_buf(buf); sid_t neighbour_sid; if (str_to_sid_t(&neighbour_sid, remainder) == -1) return 404; @@ -85,7 +85,7 @@ static int interface_page(httpd_request *r, const char *remainder) if (r->http.verb != HTTP_VERB_GET) return 405; char buf[8*1024]; - strbuf b=strbuf_local(buf, sizeof buf); + strbuf b=strbuf_local_buf(buf); int index=atoi(remainder); if (index<0 || index>=OVERLAY_MAX_INTERFACES) return 404; diff --git a/simulator.c b/simulator.c index d5ed266a..307efbe4 100644 --- a/simulator.c +++ b/simulator.c @@ -155,7 +155,7 @@ static void recv_packet(int fd, struct network *network, struct peer *destinatio DEBUGF(verbose, "New peer %s", alloca_socket_address(&addr)); struct socket_address unicast_addr; unicast_addr.local.sun_family=AF_UNIX; - strbuf d = strbuf_local(unicast_addr.local.sun_path, sizeof unicast_addr.local.sun_path); + strbuf d = strbuf_local_buf(unicast_addr.local.sun_path); static unsigned peerid=0; strbuf_sprintf(d, "%s/peer%d", network->path, peerid++); if (strbuf_overrun(d)) { @@ -413,7 +413,7 @@ static int console_create(const struct cli_parsed *parsed, struct cli_context *U struct socket_address addr; addr.local.sun_family=AF_UNIX; - strbuf b = strbuf_local(addr.local.sun_path, sizeof addr.local.sun_path); + strbuf b = strbuf_local_buf(addr.local.sun_path); strbuf_path_join(b, path, "broadcast", NULL); if (strbuf_overrun(b)) return WHY("Path too long"); diff --git a/strbuf.h b/strbuf.h index 5ad9da64..d7335a12 100644 --- a/strbuf.h +++ b/strbuf.h @@ -220,13 +220,27 @@ typedef const struct strbuf *const_strbuf; * strbuf b = strbuf_local(buf, len); * strbuf_puts(b, "some text"); * strbuf_puts(b, " some more text"); - * printf("%s\n", strbuf_str(b)); * } * * @author Andrew Bettison */ #define strbuf_local(buf,len) strbuf_init(alloca(SIZEOF_STRBUF), (buf), (len)) +/** Convenience variant of the strbuf_local() macro that computes the 'len' + * parameter from 'sizeof buf'. + * + * void print_integer(int value) { + * char temp[20]; + * strbuf b = strbuf_local_buf(temp); + * strbuf_puts(b, "["); + * strbuf_sprintf(b, "%d", value); + * strbuf_puts(b, "]"); + * printf("%s\n", temp); + * } + * + * @author Andrew Bettison + */ +#define strbuf_local_buf(buf) strbuf_local((char*)(buf), sizeof (buf)) /** Initialise a strbuf with a caller-supplied backing buffer. The current * backing buffer and its contents are forgotten, and all strbuf operations diff --git a/vomp.c b/vomp.c index ffb017d9..7c9a6d56 100644 --- a/vomp.c +++ b/vomp.c @@ -618,7 +618,7 @@ static int vomp_update_local_state(struct vomp_call_state *call, int new_state){ unsigned char our_codecs[CODEC_FLAGS_LENGTH]; char msg[256]; monitor_get_all_supported_codecs(our_codecs); - strbuf b = strbuf_local(msg, sizeof msg); + strbuf b = strbuf_local_buf(msg); strbuf_sprintf(b, "\nCODECS:%06x", call->local.session); for (i = 0; i < 256; ++i){