From 817d87522ad3d36262ac7c4b5f965049e0506923 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Tue, 12 May 2020 11:28:44 -0400 Subject: [PATCH] remove usleep(): usleep() is considered obsolete, move to nanosleep() The usleep() function is considered obsolete. The interaction of this function with SIGALRM and other timer functions such as sleep(), alarm(), setitimer(), and nanosleep() is unspecified (CWE-676) https://cwe.mitre.org/data/definitions/676.html So move to nanosleep() instead. Signed-off-by: Robin Getz --- examples/iio-monitor.c | 11 +++++++++-- iiod/ops.c | 9 ++++++--- tests/iio_stresstest.c | 10 ++++++++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/examples/iio-monitor.c b/examples/iio-monitor.c index cc242ffbd..1676bbb97 100644 --- a/examples/iio-monitor.c +++ b/examples/iio-monitor.c @@ -162,9 +162,12 @@ static void * read_thd(void *d) unsigned int i, nb_channels, nb = 0; char buf[1024]; chtype *str; + struct timespec wait; (void) row; /* Prevent warning */ - usleep(100000); + wait.tv_sec = 0; + wait.tv_nsec = (100000 * 1000); + nanosleep(&wait, &wait); if (selected < 0) continue; @@ -380,9 +383,13 @@ static void show_main_screen(struct iio_context *ctx) while (!stop) { int ret = activateCDKScroll(list, NULL); + struct timespec wait; + wait.tv_sec = 0; + wait.tv_nsec = (100000 * 1000); + stop = ret < 0; selected = ret; - usleep(100000); + nanosleep(&wait, &wait); } pthread_join(thd, NULL); diff --git a/iiod/ops.c b/iiod/ops.c index 5e91c6872..586bb165e 100644 --- a/iiod/ops.c +++ b/iiod/ops.c @@ -951,9 +951,12 @@ static int open_dev_helper(struct parser_pdata *pdata, struct iio_device *dev, * This is not pretty but it works. */ if (cyclic_retry) { - cyclic_retry--; - usleep(100); - goto retry; + struct timespec wait; + wait.tv_sec = 0; + wait.tv_nsec = (100 * 1000); + cyclic_retry--; + nanosleep(&wait, &wait); + goto retry; } ret = -EBUSY; diff --git a/tests/iio_stresstest.c b/tests/iio_stresstest.c index 30ba47ba4..eb046d9c6 100644 --- a/tests/iio_stresstest.c +++ b/tests/iio_stresstest.c @@ -318,8 +318,11 @@ static void *client_thread(void *data) info->buffers[id]++; buffer = iio_device_create_buffer(dev, info->buffer_size, false); if (!buffer) { + struct timespec wait; + wait.tv_sec = 0; + wait.tv_nsec = (1 * 1000); thread_err(id, errno, "iio_device_create_buffer failed"); - usleep(1); + nanosleep(&wait, &wait); continue; } @@ -571,7 +574,10 @@ int main(int argc, char **argv) if (info.timeout && duration >= info.timeout) { threads_running = false; } else { - usleep(1000); + struct timespec wait; + wait.tv_sec = 0; + wait.tv_nsec = (1000 * 1000); + nanosleep(&wait, &wait); } }