Skip to content

Commit

Permalink
fix compiler warnings, check returns
Browse files Browse the repository at this point in the history
fix all but one -Wmaybe-uninitialized
  • Loading branch information
Reini Urban committed Nov 12, 2024
1 parent 8241f21 commit 97ef2d6
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 50 deletions.
18 changes: 15 additions & 3 deletions src/gpio/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 23 additions & 5 deletions src/iio/iio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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*
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions src/initio/initio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';
}
Expand Down
6 changes: 4 additions & 2 deletions src/json/jsonplatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions src/mraa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/uart/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 97ef2d6

Please sign in to comment.