diff --git a/components/pthread/include/esp_pthread.h b/components/pthread/include/esp_pthread.h index 45a58da34d..4eda567fcd 100644 --- a/components/pthread/include/esp_pthread.h +++ b/components/pthread/include/esp_pthread.h @@ -63,6 +63,7 @@ esp_pthread_cfg_t esp_pthread_get_default_config(void); * @return * - ESP_OK if configuration was successfully set * - ESP_ERR_NO_MEM if out of memory + * - ESP_ERR_INVALID_ARG if cfg is NULL * - ESP_ERR_INVALID_ARG if stack_size is less than PTHREAD_STACK_MIN * - ESP_ERR_INVALID_ARG if stack_alloc_caps does not include MALLOC_CAP_8BIT */ @@ -79,6 +80,7 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg); * * @return * - ESP_OK if the configuration was available + * - ESP_ERR_INVALID_ARG if p is NULL * - ESP_ERR_NOT_FOUND if a configuration wasn't previously set */ esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p); diff --git a/components/pthread/port/linux/pthread.c b/components/pthread/port/linux/pthread.c index 107733e02e..9f1d2b40a7 100644 --- a/components/pthread/port/linux/pthread.c +++ b/components/pthread/port/linux/pthread.c @@ -52,6 +52,10 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg) { // Not checking the stack size here since PTHREAD_STACK_MIN has two conflicting declarations on Linux + if (cfg == NULL) { + return ESP_ERR_INVALID_ARG; + } + // 0 is treated as default value, hence change caps to MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL in that case int heap_caps; if (cfg->stack_alloc_caps == 0) { @@ -86,6 +90,10 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg) esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p) { + if (p == NULL) { + return ESP_ERR_INVALID_ARG; + } + esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key); if (cfg) { *p = *cfg; diff --git a/components/pthread/pthread.c b/components/pthread/pthread.c index 8d682a8972..0e73fe86bf 100644 --- a/components/pthread/pthread.c +++ b/components/pthread/pthread.c @@ -142,6 +142,10 @@ static void pthread_delete(esp_pthread_t *pthread) /* Call this function to configure pthread stacks in Pthreads */ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg) { + if (cfg == NULL) { + return ESP_ERR_INVALID_ARG; + } + if (cfg->stack_size < PTHREAD_STACK_MIN) { return ESP_ERR_INVALID_ARG; } @@ -180,6 +184,10 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg) esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p) { + if (p == NULL) { + return ESP_ERR_INVALID_ARG; + } + ESP_RETURN_ON_ERROR(lazy_init_pthread_cfg_key(), TAG, "Failed to initialize pthread key"); esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key); diff --git a/components/pthread/test_apps/pthread_unity_tests/main/test_esp_pthread.c b/components/pthread/test_apps/pthread_unity_tests/main/test_esp_pthread.c index e1ac3c25f6..08904349fc 100644 --- a/components/pthread/test_apps/pthread_unity_tests/main/test_esp_pthread.c +++ b/components/pthread/test_apps/pthread_unity_tests/main/test_esp_pthread.c @@ -18,6 +18,12 @@ TEST_CASE("esp_pthread_get_default_config creates correct stack memory capabilit TEST_ASSERT_EQUAL_HEX(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, default_config.stack_alloc_caps); } +TEST_CASE("null pointers are rejected", "[cfg]") +{ + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_set_cfg(NULL)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_get_cfg(NULL)); +} + TEST_CASE("wrong heap caps are rejected", "[cfg]") { esp_pthread_cfg_t default_config = esp_pthread_get_default_config();