Skip to content

Commit ccecd17

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 fa66688 commit ccecd17

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
@@ -227,11 +227,18 @@ static int pthread_cond_pool_init(void)
227227

228228
int pthread_condattr_init(pthread_condattr_t *att)
229229
{
230-
__ASSERT_NO_MSG(att != NULL);
231-
232230
struct posix_condattr *const attr = (struct posix_condattr *)att;
233231

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

236243
return 0;
237244
}
@@ -240,7 +247,8 @@ int pthread_condattr_destroy(pthread_condattr_t *att)
240247
{
241248
struct posix_condattr *const attr = (struct posix_condattr *)att;
242249

243-
if (attr == NULL) {
250+
if ((attr == NULL) || !attr->initialized) {
251+
LOG_DBG("%s %s initialized", "attribute", "not");
244252
return EINVAL;
245253
}
246254

@@ -254,6 +262,11 @@ int pthread_condattr_getclock(const pthread_condattr_t *ZRESTRICT att,
254262
{
255263
struct posix_condattr *const attr = (struct posix_condattr *)att;
256264

265+
if ((attr == NULL) || !attr->initialized) {
266+
LOG_DBG("%s %s initialized", "attribute", "not");
267+
return EINVAL;
268+
}
269+
257270
*clock_id = attr->clock;
258271

259272
return 0;
@@ -267,6 +280,11 @@ int pthread_condattr_setclock(pthread_condattr_t *att, clockid_t clock_id)
267280
return -EINVAL;
268281
}
269282

283+
if ((attr == NULL) || !attr->initialized) {
284+
LOG_DBG("%s %s initialized", "attribute", "not");
285+
return EINVAL;
286+
}
287+
270288
attr->clock = clock_id;
271289

272290
return 0;

0 commit comments

Comments
 (0)