Skip to content

Commit

Permalink
fix(pthread): configuration functions check for null pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjakob committed Sep 14, 2024
1 parent 7834519 commit f9e7305
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/pthread/include/esp_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions components/pthread/port/linux/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions components/pthread/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f9e7305

Please sign in to comment.