Skip to content

Commit b6a582a

Browse files
committed
posix: cond: check whether condattr is initialized in attr fns
Check whenther a pthread_condattr_t has been initialized in pthread_condattr_*() functions. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent 0535413 commit b6a582a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

lib/posix/options/cond.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,18 @@ static int pthread_cond_pool_init(void)
225225

226226
int pthread_condattr_init(pthread_condattr_t *att)
227227
{
228-
__ASSERT_NO_MSG(att != NULL);
229-
230228
struct posix_condattr *const attr = (struct posix_condattr *)att;
231229

230+
if (att == NULL) {
231+
return EINVAL;
232+
}
233+
if (attr->initialized) {
234+
LOG_DBG("%s %s initialized", "attribute", "already");
235+
return EINVAL;
236+
}
237+
232238
attr->clock = CLOCK_MONOTONIC;
239+
attr->initialized = true;
233240

234241
return 0;
235242
}
@@ -238,7 +245,8 @@ int pthread_condattr_destroy(pthread_condattr_t *att)
238245
{
239246
struct posix_condattr *const attr = (struct posix_condattr *)att;
240247

241-
if (attr == NULL) {
248+
if ((attr == NULL) || !attr->initialized) {
249+
LOG_DBG("%s %s initialized", "attribute", "not");
242250
return EINVAL;
243251
}
244252

@@ -252,6 +260,11 @@ int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
252260
{
253261
struct posix_condattr *const attr = (struct posix_condattr *)att;
254262

263+
if ((attr == NULL) || !attr->initialized) {
264+
LOG_DBG("%s %s initialized", "attribute", "not");
265+
return EINVAL;
266+
}
267+
255268
*clock_id = attr->clock;
256269

257270
return 0;
@@ -265,6 +278,11 @@ int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
265278
return -EINVAL;
266279
}
267280

281+
if ((attr == NULL) || !attr->initialized) {
282+
LOG_DBG("%s %s initialized", "attribute", "not");
283+
return EINVAL;
284+
}
285+
268286
attr->clock = clock_id;
269287

270288
return 0;

0 commit comments

Comments
 (0)