From 97ef2d67684eb0c38c621be933bf9424585d2850 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Tue, 12 Nov 2024 15:19:30 +0100 Subject: [PATCH] fix compiler warnings, check returns fix all but one -Wmaybe-uninitialized --- src/gpio/gpio.c | 18 +++++-- src/iio/iio.c | 28 ++++++++-- src/initio/initio.c | 14 ++--- src/json/jsonplatform.c | 6 ++- src/mraa.c | 16 ++++-- src/uart/uart.c | 5 +- src/x86/adlink-ipi.c | 112 ++++++++++++++++++++++++++++++++-------- src/x86/up.c | 2 +- src/x86/up2.c | 2 +- src/x86/up_xtreme.c | 2 +- tools/mraa-gpio.c | 4 +- tools/mraa-i2c.c | 4 +- 12 files changed, 163 insertions(+), 50 deletions(-) diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index f13081f51..f66c5e5d5 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -596,7 +596,11 @@ mraa_gpio_wait_interrupt(int fds[], // do an initial read to clear interrupt lseek(fds[i], 0, SEEK_SET); - read(fds[i], &c, 1); + ssize_t nbytes = read(fds[i], &c, 1); + if (nbytes < 0) { + syslog(LOG_ERR, "mraa_gpio_wait_interrupt: Failed to read from poll fd"); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } } #ifdef HAVE_PTHREAD_CANCEL @@ -614,7 +618,11 @@ mraa_gpio_wait_interrupt(int fds[], for (int i = 0; i < num_fds; ++i) { if (pfd[i].revents & POLLPRI) { - read(fds[i], &c, 1); + ssize_t nbytes = read(fds[i], &c, 1); + if (nbytes < 0) { + syslog(LOG_ERR, "mraa_gpio_wait_interrupt: Failed to read from poll fd"); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } events[i].id = i; events[i].timestamp = _mraa_gpio_get_timestamp_sysfs(); } else @@ -645,7 +653,11 @@ mraa_gpio_chardev_wait_interrupt(int fds[], int num_fds, mraa_gpio_events_t even for (int i = 0; i < num_fds; ++i) { if (pfd[i].revents & POLLIN) { - read(fds[i], &event_data, sizeof(event_data)); + ssize_t nbytes = read(fds[i], &event_data, sizeof(event_data)); + if (nbytes < 0) { + syslog(LOG_ERR, "mraa_gpio_chardev_wait_interrupt: Failed to read from poll fd"); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } events[i].id = i; events[i].timestamp = event_data.timestamp; } else diff --git a/src/iio/iio.c b/src/iio/iio.c index 26f116af3..97baf0fc3 100644 --- a/src/iio/iio.c +++ b/src/iio/iio.c @@ -111,7 +111,13 @@ mraa_iio_get_channel_data(mraa_iio_context dev) snprintf(buf, MAX_SIZE, "%stype", str); fd = open(buf, O_RDONLY); if (fd != -1) { - read(fd, readbuf, 31 * sizeof(char)); + ssize_t nbytes = read(fd, readbuf, 31 * sizeof(char)); + if (nbytes < 0) { + // cleanup + free(str); + close(fd); + return MRAA_IO_SETUP_FAILURE; + } ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf, &signchar, &chan->bits_used, &padint, &chan->shift); // probably should be 5? @@ -303,8 +309,12 @@ mraa_iio_wait_event(int fd, char* data, int* read_size) memset(data, 0, 100); *read_size = read(fd, data, 100); - - return MRAA_SUCCESS; + if (*read_size >= 0) + return MRAA_SUCCESS; + else { + syslog(LOG_ERR, "mraa_iio_wait_event: Failed to read from poll fd"); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } } static void* @@ -431,7 +441,11 @@ mraa_iio_event_poll_nonblock(int fd, struct iio_event_data* data) // poll is a cancelable point like sleep() poll(&pfd, 1, -1); - read(fd, data, sizeof(struct iio_event_data)); + ssize_t nbytes = read(fd, data, sizeof(struct iio_event_data)); + if (nbytes < 0) { + syslog(LOG_ERR, "mraa_iio_event_poll_nonblock: Failed to read iio_event_data"); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } return MRAA_SUCCESS; } @@ -456,7 +470,11 @@ mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data) if (ret == -1 || event_fd == -1) return MRAA_ERROR_UNSPECIFIED; - read(event_fd, data, sizeof(struct iio_event_data)); + ssize_t nbytes = read(event_fd, data, sizeof(struct iio_event_data)); + if (nbytes < 0) { + syslog(LOG_ERR, "mraa_iio_event_poll: Failed to read iio_event_data"); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } close(event_fd); return MRAA_SUCCESS; diff --git a/src/initio/initio.c b/src/initio/initio.c index b130f7f3a..2ac34a0ec 100644 --- a/src/initio/initio.c +++ b/src/initio/initio.c @@ -57,9 +57,10 @@ mraa_tokenize_string(const char* str, const char* delims, int* num_tokens) if (tok == NULL) { break; } + size_t tok_len = strlen(tok); output = realloc(output, (++output_size) * sizeof(char*)); - output[output_size - 1] = calloc(strlen(tok) + 1, sizeof(char)); - strncpy(output[output_size - 1], tok, strlen(tok)); + output[output_size - 1] = calloc(tok_len + 1, sizeof(char)); + strncpy(output[output_size - 1], tok, tok_len + 1); } *num_tokens = output_size; @@ -762,20 +763,21 @@ mraa_io_init(const char* strdesc, mraa_io_descriptor** desc) } } else { /* Here we build the leftover string. */ + size_t descs_len = strlen(str_descs[i]); new_desc->leftover_str = - realloc(new_desc->leftover_str, sizeof(char) * (leftover_str_len + strlen(str_descs[i]) + 2)); + realloc(new_desc->leftover_str, sizeof(char) * (leftover_str_len + descs_len) + 2); if (!new_desc->leftover_str) { syslog(LOG_ERR, "mraa_io_init: error allocating memory for leftover string"); status = MRAA_ERROR_NO_RESOURCES; free(new_desc); } else { if (leftover_str_len == 0) { - strncpy(new_desc->leftover_str, str_descs[i], strlen(str_descs[i])); + strncpy(new_desc->leftover_str, str_descs[i], descs_len); } else { - strncat(new_desc->leftover_str, str_descs[i], strlen(str_descs[i])); + strncat(new_desc->leftover_str, str_descs[i], leftover_str_len + descs_len); } - leftover_str_len += strlen(str_descs[i]) + 1; + leftover_str_len += descs_len + 1; new_desc->leftover_str[leftover_str_len - 1] = ','; new_desc->leftover_str[leftover_str_len] = '\0'; } diff --git a/src/json/jsonplatform.c b/src/json/jsonplatform.c index 93cee24d5..363efccbf 100644 --- a/src/json/jsonplatform.c +++ b/src/json/jsonplatform.c @@ -601,6 +601,7 @@ mraa_init_json_platform(const char* platform_json) int file_lock = 0, i = 0; json_object* jobj_platform = NULL; mraa_board_t* board = NULL; + size_t len; // Try to lock the file for use if ((file_lock = open(platform_json, O_RDONLY)) == -1) { @@ -703,14 +704,15 @@ mraa_init_json_platform(const char* platform_json) if (!plat->platform_name) { goto unsuccessful; } else { - platform_name = calloc(strlen(plat->platform_name) + 1, sizeof(char)); + len = strlen(plat->platform_name); + platform_name = calloc(len + 1, sizeof(char)); } if (platform_name == NULL) { syslog(LOG_ERR, "init_json_platform: Could not allocate memory for platform_name"); goto unsuccessful; } - strncpy(platform_name, plat->platform_name, strlen(plat->platform_name) + 1); + strncpy(platform_name, plat->platform_name, len + 1); // We made it to the end without anything going wrong, just cleanup ret = MRAA_SUCCESS; diff --git a/src/mraa.c b/src/mraa.c index b556d0455..e4ce72ff5 100644 --- a/src/mraa.c +++ b/src/mraa.c @@ -910,7 +910,7 @@ mraa_gpio_lookup(const char* pin_name) if (!(plat->pins[i].capabilities.gpio)) continue; - if (plat->pins[i].name != NULL && + if (*plat->pins[i].name && strncmp(pin_name, plat->pins[i].name, strlen(plat->pins[i].name) + 1) == 0) { return i; } @@ -1158,9 +1158,15 @@ mraa_find_uart_bus_pci(const char* pci_dev_path, char** dev_name) return MRAA_ERROR_INVALID_RESOURCE; } - *dev_name = (char*) malloc(sizeof(char) * max_allowable_len); + size_t len = strlen(namelist[n - 1]->d_name); + if (len > max_allowable_len) + if (n <= 0) { + syslog(LOG_ERR, "device name too long: %s", namelist[n - 1]->d_name); + return MRAA_ERROR_INVALID_RESOURCE; + } + *dev_name = (char*) malloc(sizeof(char) * len + 6); - snprintf(*dev_name, max_allowable_len, "/dev/%s", namelist[n - 1]->d_name); + snprintf(*dev_name, len + 5, "/dev/%s", namelist[n - 1]->d_name); while (n--) { free(namelist[n]); } @@ -1523,7 +1529,7 @@ mraa_init_io(const char* desc) if (length > 255 || length == 0) { return NULL; } - strncpy(buffer, desc, length); + strncpy(buffer, desc, sizeof(buffer) - 1); str = buffer; token = strsep(&str, delim); @@ -1534,7 +1540,7 @@ mraa_init_io(const char* desc) syslog(LOG_ERR, "mraa_init_io: An invalid IO type was provided"); return NULL; } - strncpy(type, token, length); + strncpy(type, token, sizeof(type) - 1); mraa_to_upper(type); token = strsep(&str, delim); // Check that they've given us more information than just the type diff --git a/src/uart/uart.c b/src/uart/uart.c index 48c07ab94..2259c786f 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -262,13 +262,14 @@ mraa_uart_init_raw(const char* path) status = MRAA_ERROR_NO_RESOURCES; goto init_raw_cleanup; } - dev->path = (char*) calloc(strlen(path)+1, sizeof(char)); + size_t path_len = strlen(path); + dev->path = (char*) calloc(path_len+1, sizeof(char)); if (dev->path == NULL) { syslog(LOG_ERR, "uart: Failed to allocate memory for path"); status = MRAA_ERROR_NO_RESOURCES; goto init_raw_cleanup; } - strncpy((char *) dev->path, path, strlen(path)); + memcpy((char*) dev->path, path, path_len + 1); if (IS_FUNC_DEFINED(dev, uart_init_raw_replace)) { status = dev->advance_func->uart_init_raw_replace(dev, path); diff --git a/src/x86/adlink-ipi.c b/src/x86/adlink-ipi.c index 849c463d3..2a16c1136 100644 --- a/src/x86/adlink-ipi.c +++ b/src/x86/adlink-ipi.c @@ -84,17 +84,32 @@ static mraa_result_t pwm_init_raw_replace(mraa_pwm_context dev, int pin) if((fd = open("/sys/class/gpio/export", O_WRONLY)) != -1) { i = snprintf(buffer, sizeof(buffer), "%d",base2 + pin); - write(fd, buffer, i); + if (write(fd, buffer, i) < 1) { + syslog(LOG_ERR, + "pwm_init_raw_replace: Failed to write %d to /sys/class/gpio/export", + base2 + pin); + return MRAA_ERROR_INVALID_RESOURCE; + } close(fd); snprintf(buffer, sizeof(buffer), "/sys/class/gpio/gpio%d/direction",base2 + pin); if((fd = open(buffer, O_WRONLY)) != -1) { - write(fd, "out", 3); + if (write(fd, "out", 3) < 3) { + syslog(LOG_ERR, + "pwm_init_raw_replace: Failed to write 'out' to %s", + buffer); + return MRAA_ERROR_INVALID_RESOURCE; + } close(fd); snprintf(buffer, sizeof(buffer), "/sys/class/gpio/gpio%d/value",base2 + pin); if((fd = open(buffer, O_WRONLY)) != -1) { - write(fd, "0", 1); + if (write(fd, "0", 1) != 1) { + syslog(LOG_ERR, + "pwm_init_raw_replace: Failed to write '0' to %s", + buffer); + return MRAA_ERROR_INVALID_RESOURCE; + } close(fd); } } @@ -227,7 +242,9 @@ void clear_sx1509x_intr(void) rx_tx_buf[0] = 0x18; rx_tx_buf[1] = 0xFF; rx_tx_buf[2] = 0xFF; - write(_fd, rx_tx_buf, 3); + if (write(_fd, rx_tx_buf, 3) != 3) { + syslog(LOG_ERR, "clear_sx1509x_intr: Failed to write"); + } } @@ -235,6 +252,7 @@ static mraa_result_t gpio_wait_interrupt(int fds[], int num_fds, mraa_gpio_event { unsigned char c; struct pollfd pfd[num_fds]; + ssize_t nbytes; pfd[0].fd = fds[0]; // setup poll on POLLPRI @@ -242,7 +260,11 @@ static mraa_result_t gpio_wait_interrupt(int fds[], int num_fds, mraa_gpio_event // do an initial read to clear interrupt lseek(fds[0], 0, SEEK_SET); - read(fds[0], &c, 1); + nbytes = read(fds[0], &c, 1); + if (nbytes < 0) { + syslog(LOG_ERR, "gpio_wait_interrupt: Failed to read from poll fd"); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } clear_sx1509x_intr(); @@ -253,7 +275,11 @@ static mraa_result_t gpio_wait_interrupt(int fds[], int num_fds, mraa_gpio_event clear_sx1509x_intr(); if (pfd[0].revents & POLLPRI) { - read(fds[0], &c, 1); + nbytes = read(fds[0], &c, 1); + if (nbytes < 0) { + syslog(LOG_ERR, "gpio_wait_interrupt: Failed to read from poll fd"); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } events[0].id = 0; events[0].timestamp = gpio_get_timestamp_sysfs(); } else @@ -319,8 +345,10 @@ static void internal_isr(void*args) // open gpio value with open(3) fps = open(it->valuepath, O_RDONLY); if (fps > 0) { - read(fps, &c, 1); + ssize_t nbytes = read(fps, &c, 1); close(fps); + if (nbytes < 0) + continue; if(it->curr != c) { (it->fptr)(it->args); @@ -360,9 +388,16 @@ static mraa_result_t intr_init() if((fd = open("/sys/class/gpio/export", O_WRONLY)) != -1) { - write(fd,"456",3); + ssize_t nbytes = write(fd,"456",3); + if (nbytes < 0) { + syslog(LOG_ERR, "intr_init: Failed to write to /sys/class/gpio/export"); + return MRAA_ERROR_INVALID_RESOURCE; + } snprintf(bu, sizeof(bu), "%d",base1 + 12); - write(fd,bu,3); + if (write(fd,bu,3) != 3) { + syslog(LOG_ERR, "intr_init: Failed to write %s", bu); + return MRAA_ERROR_INVALID_RESOURCE; + } close(fd); } else @@ -370,10 +405,15 @@ static mraa_result_t intr_init() return MRAA_ERROR_INVALID_RESOURCE; } - snprintf(bu, sizeof(bu), "/sys/class/gpio/gpio%d/direction",456); + snprintf(bu, sizeof(bu), "/sys/class/gpio/gpio%d/direction", 456); if((fd = open(bu, O_WRONLY)) != -1) { - write(fd, "in", 2); + ssize_t nbytes = write(fd, "in", 2); + if (nbytes < 0) { + syslog(LOG_ERR, "intr_init: Failed to write to /sys/class/gpio%d/direction", + 456); + return MRAA_ERROR_INVALID_RESOURCE; + } close(fd); } else @@ -381,10 +421,16 @@ static mraa_result_t intr_init() return MRAA_ERROR_INVALID_RESOURCE; } - snprintf(bu, sizeof(bu), "/sys/class/gpio/gpio%d/direction",base1 + 12); + snprintf(bu, sizeof(bu), "/sys/class/gpio/gpio%d/direction", base1 + 12); if((fd = open(bu, O_WRONLY)) != -1) { - write(fd, "in", 2); + ssize_t nbytes = write(fd, "in", 2); + if (nbytes < 0) { + syslog(LOG_ERR, + "intr_init: Failed to write to /sys/class/gpio%d/direction", + base1 + 12); + return MRAA_ERROR_INVALID_RESOURCE; + } close(fd); } else @@ -472,10 +518,17 @@ static mraa_result_t gpio_close_pre(mraa_gpio_context dev) mraa_gpio_isr_exit(gpio); if((fd = open("/sys/class/gpio/unexport", O_WRONLY)) != -1) { - length = snprintf(gpio_path, sizeof(gpio_path), "%d",gpio->pin); - write(fd, gpio_path, length); + length = snprintf(gpio_path, sizeof(gpio_path), "%d", gpio->pin); + if (write(fd, gpio_path, length) != length) { + syslog(LOG_ERR, "gpio_init_pre: Failed to write %d to /sys/class/gpio/unexport", + gpio->pin); + return MRAA_ERROR_NO_RESOURCES; + } length = snprintf(gpio_path, sizeof(gpio_path), "%d",base1 + 12); - write(fd, gpio_path, length); + if (write(fd, gpio_path, length) != length) { + syslog(LOG_ERR, "gpio_init_pre: Failed to write %s", gpio_path); + return MRAA_ERROR_NO_RESOURCES; + } close(fd); } @@ -525,7 +578,13 @@ static mraa_result_t gpio_isr_replace(mraa_gpio_context dev, mraa_gpio_edge_t mo snprintf(list->valuepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/value", list->pin); fps = open(list->valuepath, O_RDONLY); if (fps > 0) { - read(fps, &(list->curr), 1); + ssize_t nbytes = read(fps, &(list->curr), 1); + if (nbytes < 0) { + syslog(LOG_ERR, + "mraa_gpio_wait_interrupt: Failed to read from %s", + list->valuepath); + return MRAA_ERROR_NO_DATA_AVAILABLE; + } close(fps); } @@ -714,7 +773,7 @@ static mraa_result_t mraa_lec_al_set_pininfo(mraa_board_t* board, int mraa_index { if (mraa_index < board->phy_pin_count) { mraa_pininfo_t* pin_info = &board->pins[mraa_index]; - strncpy(pin_info->name, name, MRAA_PIN_NAME_SIZE); + strncpy(pin_info->name, name, MRAA_PIN_NAME_SIZE-1); pin_info->capabilities = caps; if (caps.gpio) { pin_info->gpio.pinmap = sysfs_pin; @@ -802,7 +861,10 @@ static mraa_result_t gpio_init_pre(int pin) if(read(_fd, &(rx_tx_buf[1]), 1) == 1) { rx_tx_buf[1] &= ~(1 << (pin % 8)); - write(_fd, &rx_tx_buf[0], 2); + if (write(_fd, &rx_tx_buf[0], 2) != 2) { + syslog(LOG_ERR, "gpio_init_pre: Failed to write"); + return MRAA_ERROR_NO_RESOURCES; + } } } @@ -812,11 +874,17 @@ static mraa_result_t gpio_init_pre(int pin) if(read(_fd, &(rx_tx_buf[1]), 1) == 1) { rx_tx_buf[1] &= ~(1 << (pin % 8)); - write(_fd, &rx_tx_buf[0], 2); + if (write(_fd, &rx_tx_buf[0], 2) != 2) { + syslog(LOG_ERR, "gpio_init_pre: Failed to write for pin %d", pin); + } if((fd = open("/sys/class/gpio/unexport", O_WRONLY)) != -1) { - i = snprintf(buffer, sizeof(buffer), "%d",base2 + pin); - write(fd, buffer, i); + i = snprintf(buffer, sizeof(buffer), "%d", base2 + pin); + if (write(fd, buffer, i) != i) { + syslog(LOG_ERR, + "gpio_init_pre: Failed to write %s to /sys/class/gpio/unexport", + buffer); + } close(fd); } return MRAA_SUCCESS; diff --git a/src/x86/up.c b/src/x86/up.c index 5181904cc..08e3e143e 100644 --- a/src/x86/up.c +++ b/src/x86/up.c @@ -22,7 +22,7 @@ mraa_up_set_pininfo(mraa_board_t* board, int mraa_index, char* name, mraa_pincap { if (mraa_index < board->phy_pin_count) { mraa_pininfo_t* pin_info = &board->pins[mraa_index]; - strncpy(pin_info->name, name, MRAA_PIN_NAME_SIZE); + strncpy(pin_info->name, name, MRAA_PIN_NAME_SIZE - 1); pin_info->capabilities = caps; if (caps.gpio) { pin_info->gpio.pinmap = sysfs_pin; diff --git a/src/x86/up2.c b/src/x86/up2.c index f27068723..6df7ef041 100644 --- a/src/x86/up2.c +++ b/src/x86/up2.c @@ -40,7 +40,7 @@ mraa_up2_set_pininfo(mraa_board_t* board, int mraa_index, char* name, { if (mraa_index < board->phy_pin_count) { mraa_pininfo_t* pin_info = &board->pins[mraa_index]; - strncpy(pin_info->name, name, MRAA_PIN_NAME_SIZE); + strncpy(pin_info->name, name, MRAA_PIN_NAME_SIZE-1); pin_info->capabilities = caps; if (caps.gpio) { pin_info->gpio.pinmap = sysfs_pin; diff --git a/src/x86/up_xtreme.c b/src/x86/up_xtreme.c index f1408f9c0..9f3c6ba35 100644 --- a/src/x86/up_xtreme.c +++ b/src/x86/up_xtreme.c @@ -37,7 +37,7 @@ mraa_upxtreme_set_pininfo(mraa_board_t* board, int mraa_index, char* name, { if (mraa_index < board->phy_pin_count) { mraa_pininfo_t* pin_info = &board->pins[mraa_index]; - strncpy(pin_info->name, name, MRAA_PIN_NAME_SIZE); + strncpy(pin_info->name, name, MRAA_PIN_NAME_SIZE-1); pin_info->capabilities = caps; if (caps.gpio) { pin_info->gpio.pinmap = sysfs_pin; diff --git a/tools/mraa-gpio.c b/tools/mraa-gpio.c index 882eda6c3..bb27da1f2 100644 --- a/tools/mraa-gpio.c +++ b/tools/mraa-gpio.c @@ -212,7 +212,9 @@ main(int argc, char** argv) char aux = 0; do { fflush(stdin); - fscanf(stdin, "%c", &aux); + int ret = fscanf(stdin, "%c", &aux); + if (ret == EOF) + perror("fscanf"); } while (aux != '\n'); gpio_isr_stop(&gpio_info); } else { diff --git a/tools/mraa-i2c.c b/tools/mraa-i2c.c index bd49019f7..0b8529108 100644 --- a/tools/mraa-i2c.c +++ b/tools/mraa-i2c.c @@ -228,7 +228,9 @@ run_interactive_mode() char* arg; argv[0] = "mraa-i2c"; fprintf(stdout, "Command: "); - fgets(command, 80, stdin); + char *p = fgets(command, 80, stdin); + if (!p || !*p) + return; command[strlen(command) - 1] = 0; if (strncmp(command, "q", strlen("q") + 1) == 0) return;