Skip to content

Commit 3f305ee

Browse files
committed
replace strcpy and snprintf in remaining user in libiio
https://cwe.mitre.org/data/definitions/120.html defines strcpy as potentially dangerous, and is included in Microsoft's banned.h snprintf has a iio_ special version that uses sprintf_s with Microsoft Compilers. sprintf_s guarantees that the buffer will be null-terminated unless the buffer size is zero (among other things). Linux snprintf does this by default. replace strncpy with iio_strlcpy taking advantage of mandatory null termination in iio_strlcpy replace snprintf with iio_snprintf Signed-off-by: Robin Getz <[email protected]>
1 parent db72b73 commit 3f305ee

File tree

7 files changed

+10
-12
lines changed

7 files changed

+10
-12
lines changed

dns_sd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ int dnssd_discover_host(char *addr_str, size_t addr_len, uint16_t *port)
290290

291291
if (ddata) {
292292
*port = ddata->port;
293-
strncpy(addr_str, ddata->addr_str, addr_len);
293+
iio_strlcpy(addr_str, ddata->addr_str, addr_len);
294294
}
295295

296296
host_fail:

dns_sd_bonjour.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ static void __cfnet_browser_cb (
144144
dd->port = port;
145145
dd->hostname = strdup(hostname);
146146
if (have_v4) {
147-
strncpy(dd->addr_str, address_v4, sizeof(dd->addr_str));
147+
iio_strlcpy(dd->addr_str, address_v4, sizeof(dd->addr_str));
148148
} else if(have_v6) {
149-
strncpy(dd->addr_str, address_v6, sizeof(dd->addr_str));
149+
iio_strlcpy(dd->addr_str, address_v6, sizeof(dd->addr_str));
150150
}
151151

152152
IIO_DEBUG("DNS SD: added %s (%s:%d)\n", hostname, dd->addr_str, port);

iiod/ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ ssize_t get_trigger(struct parser_pdata *pdata, struct iio_device *dev)
12621262
ret = strlen(trigger->name);
12631263
print_value(pdata, ret);
12641264

1265-
snprintf(buf, sizeof(buf), "%s\n", trigger->name);
1265+
iio_snprintf(buf, sizeof(buf), "%s\n", trigger->name);
12661266
ret = write_all(pdata, buf, ret + 1);
12671267
} else {
12681268
print_value(pdata, ret);

iiod/usbd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ static int usb_open_pipe(struct usbd_pdata *pdata, unsigned int pipe_id)
106106
* before opening the endpoints again. */
107107
thread_pool_stop_and_wait(pdata->pool[pipe_id]);
108108

109-
snprintf(buf, sizeof(buf), "%s/ep%u", pdata->ffs, pipe_id * 2 + 1);
109+
iio_snprintf(buf, sizeof(buf), "%s/ep%u", pdata->ffs, pipe_id * 2 + 1);
110110
cpdata->ep_out = open(buf, O_WRONLY);
111111
if (cpdata->ep_out < 0) {
112112
err = -errno;
113113
goto err_free_cpdata;
114114
}
115115

116-
snprintf(buf, sizeof(buf), "%s/ep%u", pdata->ffs, pipe_id * 2 + 2);
116+
iio_snprintf(buf, sizeof(buf), "%s/ep%u", pdata->ffs, pipe_id * 2 + 2);
117117
cpdata->ep_in = open(buf, O_RDONLY);
118118
if (cpdata->ep_in < 0) {
119119
err = -errno;
@@ -361,7 +361,7 @@ int start_usb_daemon(struct iio_context *ctx, const char *ffs,
361361
goto err_free_pdata_pool;
362362
}
363363

364-
snprintf(buf, sizeof(buf), "%s/ep0", ffs);
364+
iio_snprintf(buf, sizeof(buf), "%s/ep0", ffs);
365365

366366
pdata->ep0_fd = open(buf, O_RDWR);
367367
if (pdata->ep0_fd < 0) {

local.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ static int set_channel_name(struct iio_channel *chn)
223223
name = malloc(prefix_len);
224224
if (!name)
225225
return -ENOMEM;
226-
strncpy(name, attr0, prefix_len - 1);
227-
name[prefix_len - 1] = '\0';
226+
iio_strlcpy(name, attr0, prefix_len - 1);
228227
IIO_DEBUG("Setting name of channel %s to %s\n", chn->id, name);
229228
chn->name = name;
230229

network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ struct iio_context * network_create_context(const char *host)
14241424
inet_ntop(AF_INET, &in->sin_addr, description, INET_ADDRSTRLEN);
14251425
#else
14261426
char *tmp = inet_ntoa(in->sin_addr);
1427-
strncpy(description, tmp, len);
1427+
iio_strlcpy(description, tmp, len);
14281428
#endif
14291429
}
14301430

utilities.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ void iio_library_get_version(unsigned int *major,
180180
if (minor)
181181
*minor = LIBIIO_VERSION_MINOR;
182182
if (git_tag) {
183-
strncpy(git_tag, LIBIIO_VERSION_GIT, 8);
184-
git_tag[7] = '\0';
183+
iio_strlcpy(git_tag, LIBIIO_VERSION_GIT, 8);
185184
}
186185
}
187186

0 commit comments

Comments
 (0)