{
/* @todo add tests for bogus format! */
char buffer[1024];
- sprintf(buffer, "set %s 0 0 5%s\r\nvalue\r\n", key,
- noreply ? " noreply" : "");
+ snprintf(buffer, sizeof(buffer), "set %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
execute(send_string(buffer));
if (!noreply)
{
/* @todo add tests for bogus format! */
char buffer[1024];
- sprintf(buffer, "add %s 0 0 5%s\r\nvalue\r\n", key,
- noreply ? " noreply" : "");
+ snprintf(buffer, sizeof(buffer), "add %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
execute(send_string(buffer));
if (!noreply)
datasize= strlen(value);
verify(datasize < sizeof(buffer));
- sprintf(buffer, "get %s\r\n", key);
+ snprintf(buffer, sizeof(buffer), "get %s\r\n", key);
execute(send_string(buffer));
if (exist)
datasize= strlen(value);
verify(datasize < sizeof(buffer));
- sprintf(buffer, "gets %s\r\n", key);
+ snprintf(buffer, sizeof(buffer), "gets %s\r\n", key);
execute(send_string(buffer));
if (exist)
{
char buffer[300];
size_t len= strlen(value);
- sprintf(buffer, "set %s 0 0 %u\r\n", key, (unsigned int)len);
+ snprintf(buffer, sizeof(buffer), "set %s 0 0 %u\r\n", key, (unsigned int)len);
execute(send_string(buffer));
execute(retry_write(value, len));
execute(send_string("\r\n"));
static enum test_return test_ascii_replace_impl(const char* key, bool noreply)
{
char buffer[1024];
- sprintf(buffer, "replace %s 0 0 5%s\r\nvalue\r\n", key,
- noreply ? " noreply" : "");
+ snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : "");
execute(send_string(buffer));
if (noreply)
execute(ascii_set_item(key, "value"));
execute(ascii_gets_item(key, "value", true, &cas));
- sprintf(buffer, "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas,
- noreply ? " noreply" : "");
+ snprintf(buffer, sizeof(buffer), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, noreply ? " noreply" : "");
execute(send_string(buffer));
if (noreply)
execute(receive_error_response());
char buffer[1024];
- sprintf(buffer, "delete %s%s\r\n", key, noreply ? " noreply" : "");
+ snprintf(buffer, sizeof(buffer), "delete %s%s\r\n", key, noreply ? " noreply" : "");
execute(send_string(buffer));
if (noreply)
static enum test_return test_ascii_incr_impl(const char* key, bool noreply)
{
char cmd[300];
- sprintf(cmd, "incr %s 1%s\r\n", key, noreply ? " noreply" : "");
+ snprintf(cmd, sizeof(cmd), "incr %s 1%s\r\n", key, noreply ? " noreply" : "");
execute(ascii_set_item(key, "0"));
for (int x= 1; x < 11; ++x)
static enum test_return test_ascii_decr_impl(const char* key, bool noreply)
{
char cmd[300];
- sprintf(cmd, "decr %s 1%s\r\n", key, noreply ? " noreply" : "");
+ snprintf(cmd, sizeof(cmd), "decr %s 1%s\r\n", key, noreply ? " noreply" : "");
execute(ascii_set_item(key, "9"));
for (int x= 8; x > -1; --x)
value="hello";
char cmd[400];
- sprintf(cmd, "%s %s 0 0 %u%s\r\n%s\r\n",
- append ? "append" : "prepend",
- key, (unsigned int)strlen(value), noreply ? " noreply" : "",
- value);
+ snprintf(cmd, sizeof(cmd), "%s %s 0 0 %u%s\r\n%s\r\n",
+ append ? "append" : "prepend",
+ key, (unsigned int)strlen(value), noreply ? " noreply" : "",
+ value);
execute(send_string(cmd));
if (noreply)
execute(ascii_get_item(key, "hello world", true));
- sprintf(cmd, "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
- append ? "append" : "prepend",
- key, (unsigned int)strlen(value), noreply ? " noreply" : "",
- value);
+ snprintf(cmd, sizeof(cmd), "%s %s_notfound 0 0 %u%s\r\n%s\r\n",
+ append ? "append" : "prepend",
+ key, (unsigned int)strlen(value), noreply ? " noreply" : "",
+ value);
execute(send_string(cmd));
if (noreply)
char buf[1024];
char *pos= buf;
- pos+= sprintf(pos, "cmd_get: %zu\n",
- ms_stats.cmd_get);
- pos+= sprintf(pos, "cmd_set: %zu\n",
- ms_stats.cmd_set);
- pos+= sprintf(pos, "get_misses: %zu\n",
- ms_stats.get_misses);
+ pos+= snprintf(pos,
+ sizeof(buf), "cmd_get: %lu\n",
+ (unsigned long) ms_stats.cmd_get);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "cmd_set: %lu\n",
+ (unsigned long) ms_stats.cmd_set);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "get_misses: %lu\n",
+ (unsigned long) ms_stats.get_misses);
if (ms_setting.verify_percent > 0)
{
- pos+= sprintf(pos, "verify_misses: %zu\n",
- ms_stats.vef_miss);
- pos+= sprintf(pos, "verify_failed: %zu\n",
- ms_stats.vef_failed);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "verify_misses: %lu\n",
+ (unsigned long) ms_stats.vef_miss);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "verify_failed: %lu\n",
+ (unsigned long) ms_stats.vef_failed);
}
if (ms_setting.exp_ver_per > 0)
{
- pos+= sprintf(pos, "expired_get: %zu\n",
- ms_stats.exp_get);
- pos+= sprintf(pos, "unexpired_unget: %zu\n",
- ms_stats.unexp_unget);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "expired_get: %lu\n",
+ (unsigned long) ms_stats.exp_get);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "unexpired_unget: %lu\n",
+ (unsigned long) ms_stats.unexp_unget);
}
- pos+= sprintf(pos,
- "written_bytes: %zu\n",
- ms_stats.bytes_written);
- pos+= sprintf(pos, "read_bytes: %zu\n",
- ms_stats.bytes_read);
- pos+= sprintf(pos, "object_bytes: %zu\n",
- ms_stats.obj_bytes);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "written_bytes: %lu\n",
+ (unsigned long) ms_stats.bytes_written);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "read_bytes: %lu\n",
+ (unsigned long) ms_stats.bytes_read);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "object_bytes: %lu\n",
+ (unsigned long) ms_stats.obj_bytes);
if (ms_setting.udp || ms_setting.facebook_test)
{
- pos+= sprintf(pos, "packet_disorder: %zu\n",
- ms_stats.pkt_disorder);
- pos+= sprintf(pos, "packet_drop: %zu\n",
- ms_stats.pkt_drop);
- pos+= sprintf(pos, "udp_timeout: %zu\n",
- ms_stats.udp_timeout);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "packet_disorder: %lu\n",
+ (unsigned long) ms_stats.pkt_disorder);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "packet_drop: %lu\n",
+ (unsigned long)ms_stats.pkt_drop);
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "udp_timeout: %lu\n",
+ (unsigned long)ms_stats.udp_timeout);
}
if (ms_setting.stat_freq > 0)
}
int64_t time_diff= ms_time_diff(start_time, end_time);
- pos+= sprintf(
- pos,
- "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
- (double)time_diff / 1000000,
- (unsigned long long)(ms_stats.cmd_get + ms_stats.cmd_set),
- (ms_stats.cmd_get
- + ms_stats.cmd_set) / ((long double)time_diff / 1000000),
- (double)(
- ms_stats.bytes_written
- + ms_stats.bytes_read) / 1024 / 1024
- / ((double)time_diff / 1000000));
+ pos+= snprintf(pos,
+ sizeof(buf) - (size_t)(pos -buf),
+ "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n",
+ (double)time_diff / 1000000,
+ (unsigned long long)(ms_stats.cmd_get + ms_stats.cmd_set),
+ (ms_stats.cmd_get
+ + ms_stats.cmd_set) / ((long double)time_diff / 1000000),
+ (double)(
+ ms_stats.bytes_written
+ + ms_stats.bytes_read) / 1024 / 1024
+ / ((double)time_diff / 1000000));
+ assert(pos <= buf);
fprintf(stdout, "%s", buf);
fflush(stdout);
int write_len;
char *buffer= c->wbuf;
- write_len= sprintf(buffer,
- " %u %d %d\r\n",
- 0,
- item->exp_time,
- item->value_size);
+ write_len= snprintf(buffer,
+ c->wsize,
+ " %u %d %d\r\n",
+ 0,
+ item->exp_time,
+ item->value_size);
- if (write_len > c->wsize)
+ if (write_len > c->wsize || write_len < 0)
{
/* ought to be always enough. just fail for simplicity */
fprintf(stderr, "output command line too long.\n");
* @param raddr remote address (out)
* @return true on success false otherwise (errno contains more info)
*/
-static bool resolve_names(int fd, char *laddr, char *raddr)
+static bool resolve_names(int fd, char *laddr, size_t laddr_length, char *raddr, size_t raddr_length)
{
char host[NI_MAXHOST];
char port[NI_MAXSERV];
return false;
}
- (void)sprintf(laddr, "%s;%s", host, port);
+ (void)snprintf(laddr, laddr_length, "%s;%s", host, port);
salen= sizeof(saddr);
if ((getpeername(fd, (struct sockaddr *)&saddr, &salen) < 0) ||
return false;
}
- (void)sprintf(raddr, "%s;%s", host, port);
+ (void)snprintf(raddr, raddr_length, "%s;%s", host, port);
return true;
}
char laddr[NI_MAXHOST + NI_MAXSERV];
char raddr[NI_MAXHOST + NI_MAXSERV];
- unlikely (!resolve_names(server->fd, laddr, raddr))
+ unlikely (!resolve_names(server->fd, laddr, sizeof(laddr), raddr, sizeof(raddr)))
{
server->cached_errno= errno;
return MEMCACHED_ERRNO;
{
char key[16];
- sprintf(key, "foo%u", x);
+ snprintf(key, sizeof(key), "foo%u", x);
rc= memcached_set(memc, key, strlen(key),
value, value_length,
for (int x= 0; x < 10000; x++)
{
char key[10];
- sprintf(key, "%d", x);
+ snprintf(key, sizeof(key), "%d", x);
uint32_t server_idx = memcached_generate_hash(memc, key, strlen(key));
char *hostname = memc->hosts[server_idx].hostname;
for (uint32_t x= 0; x < construct->count; x++)
{
- int count;
int status;
in_port_t port;
status= system(buffer);
fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
}
- count= sprintf(end_ptr, "localhost:%u,", port);
+ int count;
+ size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer);
+ count= snprintf(end_ptr, remaining_length, "localhost:%u,", port);
+
+ if ((size_t)count >= remaining_length || count < 0)
+ {
+ fprintf(stderr, "server names grew to be larger then buffer allowed\n");
+ abort();
+ }
end_ptr+= count;
}
*end_ptr= 0;