Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mraa-gpio: return proper error codes #1130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions src/led/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mraa_led_get_brightfd(mraa_led_context dev)
snprintf(buf, MAX_SIZE, "%s/%s", dev->led_path, "brightness");
dev->bright_fd = open(buf, O_RDWR);
if (dev->bright_fd == -1) {
syslog(LOG_ERR, "led: brightness: Failed to open 'brightness': %s", strerror(errno));
syslog(LOG_ERR, "led: brightness: Failed to open %s: %s", buf, strerror(errno));
return MRAA_ERROR_INVALID_RESOURCE;
}

Expand All @@ -59,7 +59,7 @@ mraa_led_get_maxbrightfd(mraa_led_context dev)
snprintf(buf, MAX_SIZE, "%s/%s", dev->led_path, "max_brightness");
dev->max_bright_fd = open(buf, O_RDONLY);
if (dev->max_bright_fd == -1) {
syslog(LOG_ERR, "led: max_brightness: Failed to open 'max_brightness': %s", strerror(errno));
syslog(LOG_ERR, "led: max_brightness: Failed to open %s: %s", buf, strerror(errno));
return MRAA_ERROR_INVALID_RESOURCE;
}

Expand Down Expand Up @@ -119,7 +119,7 @@ mraa_led_init_internal(const char* led)

snprintf(brightness_path, sizeof(brightness_path), "%s/%s", led_path, "brightness");
if (access(brightness_path, R_OK | W_OK) != 0) {
syslog(LOG_NOTICE, "led: init: current user doesn't have access rights for using LED %s", led_name);
syslog(LOG_NOTICE, "led: init: current user doesn't have access rights for %s", brightness_path);
}

closedir(dir);
Expand Down
19 changes: 12 additions & 7 deletions src/mraa.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,8 @@ mraa_iio_detect()
// remove any trailing CR/LF symbols
name[strcspn(name, "\r\n")] = '\0';
len = strlen(name);
// use strndup
device->name = malloc((sizeof(char) * len) + sizeof(char));
strncpy(device->name, name, len + 1);
memcpy(device->name, name, len + 1);
}
close(fd);
}
Expand Down Expand Up @@ -910,7 +909,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 +1157,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 +1528,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 +1539,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