mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-30 18:57:19 +02:00
driver: Fix ana_cmpr negative enum comparison
The C17 standard (sec 6.7.2.2) indicates that the underlying type of an enum is implementation defined (i.e., can be signed or unsigned). Thus, comparing "-1 >= some_enum" where "some_enum" is always 0 or largert can return true if the compiler uses unsigned for enums. This commit fixes the following issues with ana_cmpr: - Fixed incorrect comparison in ana_cmpr_del_unit() that relied on enums being signed, thus would always return true. - Fixed incorrect expected argument in the "ana_cmpr_unit_install_uninstall" test. This was not picked up due to the incorrect enum comparison above.
This commit is contained in:
@ -155,14 +155,14 @@ esp_err_t ana_cmpr_del_unit(ana_cmpr_handle_t cmpr)
|
|||||||
{
|
{
|
||||||
ANA_CMPR_NULL_POINTER_CHECK(cmpr);
|
ANA_CMPR_NULL_POINTER_CHECK(cmpr);
|
||||||
/* Search the global object array to check if the input handle is valid */
|
/* Search the global object array to check if the input handle is valid */
|
||||||
ana_cmpr_unit_t unit = -1;
|
int unit = -1;
|
||||||
for (int i = 0; i < SOC_ANA_CMPR_NUM; i++) {
|
for (int i = 0; i < SOC_ANA_CMPR_NUM; i++) {
|
||||||
if (s_ana_cmpr[i] == cmpr) {
|
if (s_ana_cmpr[i] == cmpr) {
|
||||||
unit = i;
|
unit = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ESP_RETURN_ON_FALSE(unit >= ANA_CMPR_UNIT_0, ESP_ERR_INVALID_ARG, TAG, "wrong analog comparator handle");
|
ESP_RETURN_ON_FALSE(unit != -1, ESP_ERR_INVALID_ARG, TAG, "wrong analog comparator handle");
|
||||||
ESP_RETURN_ON_FALSE(!cmpr->is_enabled, ESP_ERR_INVALID_STATE, TAG, "this analog comparator unit not disabled yet");
|
ESP_RETURN_ON_FALSE(!cmpr->is_enabled, ESP_ERR_INVALID_STATE, TAG, "this analog comparator unit not disabled yet");
|
||||||
|
|
||||||
/* Delete the pm lock if the unit has */
|
/* Delete the pm lock if the unit has */
|
||||||
|
@ -37,7 +37,7 @@ TEST_CASE("ana_cmpr_unit_install_uninstall", "[ana_cmpr]")
|
|||||||
/* Disable the unit */
|
/* Disable the unit */
|
||||||
TEST_ESP_OK(ana_cmpr_disable(cmpr));
|
TEST_ESP_OK(ana_cmpr_disable(cmpr));
|
||||||
/* Try to delete the unit with a wrong handle */
|
/* Try to delete the unit with a wrong handle */
|
||||||
TEST_ESP_ERR(ESP_ERR_INVALID_STATE, ana_cmpr_del_unit((void *)&cmpr));
|
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, ana_cmpr_del_unit((void *)&cmpr));
|
||||||
/* Delete the unit */
|
/* Delete the unit */
|
||||||
TEST_ESP_OK(ana_cmpr_del_unit(cmpr));
|
TEST_ESP_OK(ana_cmpr_del_unit(cmpr));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user