Skip to content

Commit f9e7305

Browse files
committed
fix(pthread): configuration functions check for null pointer
1 parent 7834519 commit f9e7305

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

components/pthread/include/esp_pthread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ esp_pthread_cfg_t esp_pthread_get_default_config(void);
6363
* @return
6464
* - ESP_OK if configuration was successfully set
6565
* - ESP_ERR_NO_MEM if out of memory
66+
* - ESP_ERR_INVALID_ARG if cfg is NULL
6667
* - ESP_ERR_INVALID_ARG if stack_size is less than PTHREAD_STACK_MIN
6768
* - ESP_ERR_INVALID_ARG if stack_alloc_caps does not include MALLOC_CAP_8BIT
6869
*/
@@ -79,6 +80,7 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg);
7980
*
8081
* @return
8182
* - ESP_OK if the configuration was available
83+
* - ESP_ERR_INVALID_ARG if p is NULL
8284
* - ESP_ERR_NOT_FOUND if a configuration wasn't previously set
8385
*/
8486
esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p);

components/pthread/port/linux/pthread.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg)
5252
{
5353
// Not checking the stack size here since PTHREAD_STACK_MIN has two conflicting declarations on Linux
5454

55+
if (cfg == NULL) {
56+
return ESP_ERR_INVALID_ARG;
57+
}
58+
5559
// 0 is treated as default value, hence change caps to MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL in that case
5660
int heap_caps;
5761
if (cfg->stack_alloc_caps == 0) {
@@ -86,6 +90,10 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg)
8690

8791
esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p)
8892
{
93+
if (p == NULL) {
94+
return ESP_ERR_INVALID_ARG;
95+
}
96+
8997
esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key);
9098
if (cfg) {
9199
*p = *cfg;

components/pthread/pthread.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ static void pthread_delete(esp_pthread_t *pthread)
142142
/* Call this function to configure pthread stacks in Pthreads */
143143
esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg)
144144
{
145+
if (cfg == NULL) {
146+
return ESP_ERR_INVALID_ARG;
147+
}
148+
145149
if (cfg->stack_size < PTHREAD_STACK_MIN) {
146150
return ESP_ERR_INVALID_ARG;
147151
}
@@ -180,6 +184,10 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg)
180184

181185
esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p)
182186
{
187+
if (p == NULL) {
188+
return ESP_ERR_INVALID_ARG;
189+
}
190+
183191
ESP_RETURN_ON_ERROR(lazy_init_pthread_cfg_key(), TAG, "Failed to initialize pthread key");
184192

185193
esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key);

components/pthread/test_apps/pthread_unity_tests/main/test_esp_pthread.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ TEST_CASE("esp_pthread_get_default_config creates correct stack memory capabilit
1818
TEST_ASSERT_EQUAL_HEX(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, default_config.stack_alloc_caps);
1919
}
2020

21+
TEST_CASE("null pointers are rejected", "[cfg]")
22+
{
23+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_set_cfg(NULL));
24+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_get_cfg(NULL));
25+
}
26+
2127
TEST_CASE("wrong heap caps are rejected", "[cfg]")
2228
{
2329
esp_pthread_cfg_t default_config = esp_pthread_get_default_config();

0 commit comments

Comments
 (0)