Using strncpy() to copy strings is suboptimal because strncpy writes a
bunch of additional unnecessary null bytes. Use snprintf() instead of
strncpy(). An additional advantage of snprintf() is that it guarantees
that the output string is '\0'-terminated.
This patch is an improvement for commit
32e31c8c5f7b ("Fix string copy
compilation warnings").
Cc: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
14 files changed:
static void __string_to_net(uint8_t *dst, const char *src, size_t dst_size)
{
static void __string_to_net(uint8_t *dst, const char *src, size_t dst_size)
{
- if (src) {
- dst[dst_size - 1] = '\0';
- strncpy((char *) dst, src, dst_size - 1);
- } else
+ if (src)
+ snprintf((char *) dst, dst_size, "%s", src);
+ else
sname = server_name(client, buf, sizeof(buf));
memset(pdu.server, 0, sizeof(pdu.server));
sname = server_name(client, buf, sizeof(buf));
memset(pdu.server, 0, sizeof(pdu.server));
- strncpy((char *) pdu.server, sname, sizeof(pdu.server) - 1);
+ snprintf((char *) pdu.server, sizeof(pdu.server), "%s", sname);
fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
}
fio_net_send_cmd(client->fd, FIO_NET_CMD_PROBE, &pdu, sizeof(pdu), &tag, &client->cmd_list);
}
memset(addr, 0, sizeof(*addr));
addr->sun_family = AF_UNIX;
memset(addr, 0, sizeof(*addr));
addr->sun_family = AF_UNIX;
- strncpy(addr->sun_path, client->hostname, sizeof(addr->sun_path) - 1);
+ snprintf(addr->sun_path, sizeof(addr->sun_path), "%s",
+ client->hostname);
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
/*
* must be a file, open "." in that path
*/
/*
* must be a file, open "." in that path
*/
- tempname[PATH_MAX - 1] = '\0';
- strncpy(tempname, file_name, PATH_MAX - 1);
+ snprintf(tempname, ARRAY_SIZE(tempname), "%s", file_name);
p = dirname(tempname);
if (stat(p, &st)) {
perror("disk util stat");
p = dirname(tempname);
if (stat(p, &st)) {
perror("disk util stat");
sfree(du);
return NULL;
}
sfree(du);
return NULL;
}
- strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ - 1);
+ snprintf((char *) du->dus.name, ARRAY_SIZE(du->dus.name), "%s",
+ basename(path));
du->sysfs_root = strdup(path);
du->major = majdev;
du->minor = mindev;
du->sysfs_root = strdup(path);
du->major = majdev;
du->minor = mindev;
log_err("unknown sysfs layout\n");
return NULL;
}
log_err("unknown sysfs layout\n");
return NULL;
}
- tmp[PATH_MAX - 1] = '\0';
- strncpy(tmp, p, PATH_MAX - 1);
+ snprintf(tmp, ARRAY_SIZE(tmp), "%s", p);
sprintf(path, "%s", tmp);
}
sprintf(path, "%s", tmp);
}
struct sockaddr_un *soun = &nd->addr_un;
soun->sun_family = AF_UNIX;
struct sockaddr_un *soun = &nd->addr_un;
soun->sun_family = AF_UNIX;
- memset(soun->sun_path, 0, sizeof(soun->sun_path));
- strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
+ snprintf(soun->sun_path, sizeof(soun->sun_path), "%s", path);
- memset(addr, 0, sizeof(*addr));
addr->sun_family = AF_UNIX;
addr->sun_family = AF_UNIX;
- strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
+ snprintf(addr->sun_path, sizeof(addr->sun_path), "%s", path);
unlink(path);
len = sizeof(addr->sun_family) + strlen(path) + 1;
unlink(path);
len = sizeof(addr->sun_family) + strlen(path) + 1;
}
if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
}
if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
- strncpy(msg, "SG Driver did not report a Host, Driver or Device check",
- MAXERRDETAIL - 1);
+ snprintf(msg, MAXERRDETAIL, "%s",
+ "SG Driver did not report a Host, Driver or Device check");
} else if (f->filetype != FIO_TYPE_FILE)
continue;
} else if (f->filetype != FIO_TYPE_FILE)
continue;
- buf[255] = '\0';
- strncpy(buf, f->file_name, 255);
+ snprintf(buf, ARRAY_SIZE(buf), "%s", f->file_name);
if (stat(buf, &sb) < 0) {
if (errno != ENOENT)
if (stat(buf, &sb) < 0) {
if (errno != ENOENT)
continue;
fm = calloc(1, sizeof(*fm));
continue;
fm = calloc(1, sizeof(*fm));
- strncpy(fm->__base, buf, sizeof(fm->__base));
- fm->__base[255] = '\0';
+ snprintf(fm->__base, ARRAY_SIZE(fm->__base), "%s", buf);
fm->base = basename(fm->__base);
fm->key = sb.st_dev;
flist_add(&fm->list, &list);
fm->base = basename(fm->__base);
fm->key = sb.st_dev;
flist_add(&fm->list, &list);
static char message[100];
const char *m = message;
static char message[100];
const char *m = message;
- strncpy(message, status_message, sizeof(message) - 1);
+ snprintf(message, sizeof(message), "%s", status_message);
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ge->thread_status_pb), m);
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ge->thread_status_pb), perc / 100.0);
gtk_widget_queue_draw(ge->ui->window);
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ge->thread_status_pb), m);
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ge->thread_status_pb), perc / 100.0);
gtk_widget_queue_draw(ge->ui->window);
static char message[100];
const char *m = message;
static char message[100];
const char *m = message;
- strncpy(message, status_message, sizeof(message) - 1);
+ strncpy(message, sizeof(message), "%s", status_message);
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ui->thread_status_pb), m);
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui->thread_status_pb), perc / 100.0);
gtk_widget_queue_draw(ui->window);
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ui->thread_status_pb), m);
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui->thread_status_pb), perc / 100.0);
gtk_widget_queue_draw(ui->window);
for (f = &fpre_keywords[0]; f->keyword; f++)
f->strlen = strlen(f->keyword);
for (f = &fpre_keywords[0]; f->keyword; f++)
f->strlen = strlen(f->keyword);
- buf[buf_size - 1] = '\0';
- strncpy(buf, o->filename_format, buf_size - 1);
+ snprintf(buf, buf_size, "%s", o->filename_format);
memset(copy, 0, sizeof(copy));
for (f = &fpre_keywords[0]; f->keyword; f++) {
memset(copy, 0, sizeof(copy));
for (f = &fpre_keywords[0]; f->keyword; f++) {
if (post_start)
strncpy(dst, buf + post_start, dst_left);
if (post_start)
strncpy(dst, buf + post_start, dst_left);
- strncpy(buf, copy, buf_size - 1);
+ snprintf(buf, buf_size, "%s", copy);
*/
if (access(filename, F_OK) &&
(ts = strrchr(file, '/'))) {
*/
if (access(filename, F_OK) &&
(ts = strrchr(file, '/'))) {
- int len = ts - file +
- strlen(filename) + 2;
-
- if (!(full_fn = calloc(1, len))) {
+ if (asprintf(&full_fn, "%.*s%s",
+ (int)(ts - file + 1), file,
+ filename) < 0) {
-
- strncpy(full_fn,
- file, (ts - file) + 1);
- strncpy(full_fn + (ts - file) + 1,
- filename,
- len - (ts - file) - 1);
- full_fn[len - 1] = 0;
- engine[sizeof(engine) - 1] = '\0';
- strncpy(engine, name, sizeof(engine) - 1);
+ snprintf(engine, sizeof(engine), "%s", name);
/*
* linux libaio has alias names, so convert to what we want
/*
* linux libaio has alias names, so convert to what we want
- in[OPT_LEN_MAX] = '\0';
- strncpy(in, opt, OPT_LEN_MAX);
+ snprintf(in, sizeof(in), "%s", opt);
while (*inptr && nchr > 0) {
if (inptr[0] == '$' && inptr[1] == '{') {
while (*inptr && nchr > 0) {
if (inptr[0] == '$' && inptr[1] == '{') {
if (!is_time && o->is_time)
is_time = o->is_time;
if (!is_time && o->is_time)
is_time = o->is_time;
- tmp[sizeof(tmp) - 1] = '\0';
- strncpy(tmp, ptr, sizeof(tmp) - 1);
+ snprintf(tmp, sizeof(tmp), "%s", ptr);
p = strchr(tmp, ',');
if (p)
*p = '\0';
p = strchr(tmp, ',');
if (p)
*p = '\0';
char tmp[128];
char *p1, *p2;
char tmp[128];
char *p1, *p2;
- tmp[sizeof(tmp) - 1] = '\0';
- strncpy(tmp, ptr, sizeof(tmp) - 1);
+ snprintf(tmp, sizeof(tmp), "%s", ptr);
/* Handle bsrange with separate read,write values: */
p1 = strchr(tmp, ',');
/* Handle bsrange with separate read,write values: */
p1 = strchr(tmp, ',');
strcpy(me, (char *) pdu->server);
gethostname((char *) probe.hostname, sizeof(probe.hostname));
strcpy(me, (char *) pdu->server);
gethostname((char *) probe.hostname, sizeof(probe.hostname));
- strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version) - 1);
+ snprintf((char *) probe.fio_version, sizeof(probe.fio_version), "%s",
+ fio_version_string);
/*
* If the client supports compression and we do too, then enable it
/*
* If the client supports compression and we do too, then enable it
memset(&p, 0, sizeof(p));
memset(&p, 0, sizeof(p));
- strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE);
- p.ts.name[FIO_JOBNAME_SIZE - 1] = '\0';
- strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE);
- p.ts.verror[FIO_VERROR_SIZE - 1] = '\0';
- strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE);
- p.ts.description[FIO_JOBDESC_SIZE - 1] = '\0';
+ snprintf(p.ts.name, sizeof(p.ts.name), "%s", ts->name);
+ snprintf(p.ts.verror, sizeof(p.ts.verror), "%s", ts->verror);
+ snprintf(p.ts.description, sizeof(p.ts.description), "%s",
+ ts->description);
p.ts.error = cpu_to_le32(ts->error);
p.ts.thread_number = cpu_to_le32(ts->thread_number);
p.ts.error = cpu_to_le32(ts->error);
p.ts.thread_number = cpu_to_le32(ts->thread_number);
- dst->name[FIO_DU_NAME_SZ - 1] = '\0';
- strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
+ snprintf((char *) dst->name, sizeof(dst->name), "%s", src->name);
for (i = 0; i < 2; i++) {
dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
for (i = 0; i < 2; i++) {
dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
- strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
- pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
+ snprintf((char *) pdu.name, sizeof(pdu.name), "%s", name);
/*
* We can't do this for a pre-compressed log, but for that case,
/*
* We can't do this for a pre-compressed log, but for that case,
- memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
addr.sun_family = AF_UNIX;
- strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
+ snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", bind_sock);
len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
if (p)
strcat(p, port);
else
if (p)
strcat(p, port);
else
- strncpy(bind_str, port, sizeof(bind_str) - 1);
+ snprintf(bind_str, sizeof(bind_str), "%s", port);
- strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
+ snprintf(bind_str, sizeof(bind_str), "%s", bind_sock);
log_info("fio: server listening on %s\n", bind_str);
log_info("fio: server listening on %s\n", bind_str);
/*
* These are per-group shared already
*/
/*
* These are per-group shared already
*/
- strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE - 1);
+ snprintf(ts->name, sizeof(ts->name), "%s", td->o.name);
- strncpy(ts->description, td->o.description,
- FIO_JOBDESC_SIZE - 1);
+ snprintf(ts->description,
+ sizeof(ts->description), "%s",
+ td->o.description);
else
memset(ts->description, 0, FIO_JOBDESC_SIZE);
else
memset(ts->description, 0, FIO_JOBDESC_SIZE);
if (!td->error && td->o.continue_on_error &&
td->first_error) {
ts->error = td->first_error;
if (!td->error && td->o.continue_on_error &&
td->first_error) {
ts->error = td->first_error;
- ts->verror[sizeof(ts->verror) - 1] = '\0';
- strncpy(ts->verror, td->verror, sizeof(ts->verror) - 1);
+ snprintf(ts->verror, sizeof(ts->verror), "%s",
+ td->verror);
} else if (td->error) {
ts->error = td->error;
} else if (td->error) {
ts->error = td->error;
- ts->verror[sizeof(ts->verror) - 1] = '\0';
- strncpy(ts->verror, td->verror, sizeof(ts->verror) - 1);
+ snprintf(ts->verror, sizeof(ts->verror), "%s",
+ td->verror);
s->rand.state32.s[3] = 0;
s->rand.use64 = 0;
}
s->rand.state32.s[3] = 0;
s->rand.use64 = 0;
}
- s->name[sizeof(s->name) - 1] = '\0';
- strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
+ snprintf((char *) s->name, sizeof(s->name), "%s", td->o.name);
next = io_list_next(s);
}
next = io_list_next(s);
}