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

Missing error check of device_get_binding() and flash_area_open() #30195

Closed
Hxinrong opened this issue Nov 23, 2020 · 1 comment
Closed

Missing error check of device_get_binding() and flash_area_open() #30195

Hxinrong opened this issue Nov 23, 2020 · 1 comment
Labels
area: Drivers area: Tests Issues related to a particular existing or missing test bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug Stale

Comments

@Hxinrong
Copy link
Contributor

Hxinrong commented Nov 23, 2020

Hi,
As described in the API documentation, function device_get_binding() is used to retrieve the device structure for a driver by name, and returns the pointer to device structure, or NULL if not found or cannot be used. But as shown in the following three call sites, the error checks are not made.

uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
uart_console_hook_install();
return 0;

driver->cs_ctrl.gpio_dev = device_get_binding(
DT_INST_SPI_DEV_CS_GPIOS_LABEL(0));

data->cs_ctrl.gpio_dev = device_get_binding(
DT_INST_SPI_DEV_CS_GPIOS_LABEL(0));

Moreover, in other sites, the return values of device_get_binding() have been checked for NULL. For instance,

data->cs_ctrl.gpio_dev =
device_get_binding(DT_INST_SPI_DEV_CS_GPIOS_LABEL(0));
if (!data->cs_ctrl.gpio_dev) {
return -ENODEV;
}

driver->cs_ctrl.gpio_dev = device_get_binding(GD7965_CS_CNTRL);
if (!driver->cs_ctrl.gpio_dev) {
LOG_ERR("Unable to get SPI GPIO CS device");
return -EIO;
}

===================================================================================
Also, function flash_area_open() is used to retrieve partitions flash area from the flash_map, and returns 0 on success, -EACCES if the flash_map is not available , or -ENOENT if ID is unknown. But in the following codes, the return value of it is not checked before the call of flash_area_erase().

rc = flash_area_open(FLASH_AREA_ID(storage), &fap);
for (i = 0; i < cnt; i++) {
rc = flash_area_erase(fap, fs[i].fs_off, fs[i].fs_size);
zassert_true(rc == 0, "Can't get flash area");
}

However, in other parts, the return values are error checked. As shown in the following codes,

int rc = flash_area_open(FLASH_AREA_ID(storage), &fap);
if (rc == 0) {
rc = flash_area_erase(fap, 0, fap->fa_size);
flash_area_close(fap);
}

int rc = flash_area_open(id, &pfa);
if (rc < 0) {
TC_PRINT("Error accessing flash area %u [%d]\n",
id, rc);
return TC_FAIL;
}

@Hxinrong Hxinrong added the bug The issue is a bug, or the PR is fixing a bug label Nov 23, 2020
@de-nordic de-nordic added area: Drivers area: Tests Issues related to a particular existing or missing test labels Nov 23, 2020
@nashif nashif added the priority: low Low impact/importance bug label Dec 1, 2020
@github-actions
Copy link

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

@github-actions github-actions bot added the Stale label Jan 31, 2021
Hxinrong added a commit to Hxinrong/zephyr that referenced this issue Feb 22, 2021
Validate that flash_area_open() succeeds.

Fixes zephyrproject-rtos#30195

Signed-off-by: Xinrong Han <hanxr19@mails.tsinghua.edu.cn>
Hxinrong added a commit to Hxinrong/zephyr that referenced this issue Feb 22, 2021
Log error info when device_get_binding() is NULL.

Fixes zephyrproject-rtos#30195

Signed-off-by: Xinrong Han hanxr19@mails.tsinghua.edu.cn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Drivers area: Tests Issues related to a particular existing or missing test bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug Stale
Projects
None yet
Development

No branches or pull requests

3 participants