fix(pthread): configuration functions check for null pointer

This commit is contained in:
Jakob Hasse 2024-09-09 12:14:05 +02:00
parent 7834519af8
commit f9e7305efd
4 changed files with 24 additions and 0 deletions

View File

@ -63,6 +63,7 @@ esp_pthread_cfg_t esp_pthread_get_default_config(void);
* @return * @return
* - ESP_OK if configuration was successfully set * - ESP_OK if configuration was successfully set
* - ESP_ERR_NO_MEM if out of memory * - 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_size is less than PTHREAD_STACK_MIN
* - ESP_ERR_INVALID_ARG if stack_alloc_caps does not include MALLOC_CAP_8BIT * - 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 * @return
* - ESP_OK if the configuration was available * - 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_NOT_FOUND if a configuration wasn't previously set
*/ */
esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p); esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p);

View File

@ -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 // 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 // 0 is treated as default value, hence change caps to MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL in that case
int heap_caps; int heap_caps;
if (cfg->stack_alloc_caps == 0) { 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) 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); esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key);
if (cfg) { if (cfg) {
*p = *cfg; *p = *cfg;

View File

@ -142,6 +142,10 @@ static void pthread_delete(esp_pthread_t *pthread)
/* Call this function to configure pthread stacks in Pthreads */ /* Call this function to configure pthread stacks in Pthreads */
esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg) 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) { if (cfg->stack_size < PTHREAD_STACK_MIN) {
return ESP_ERR_INVALID_ARG; 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) 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_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); esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key);

View File

@ -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_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]") TEST_CASE("wrong heap caps are rejected", "[cfg]")
{ {
esp_pthread_cfg_t default_config = esp_pthread_get_default_config(); esp_pthread_cfg_t default_config = esp_pthread_get_default_config();