mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-18 08:20:44 +00:00
LibPthread: Ensure we're not overflowing the semaphore's value
This commit is contained in:
parent
62ced35346
commit
98403eccb0
Notes:
sideshowbarker
2024-07-18 20:19:43 +09:00
Author: https://github.com/gunnarbeutner
Commit: 98403eccb0
Pull-request: https://github.com/SerenityOS/serenity/pull/6332
Reviewed-by: https://github.com/ADKaster
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/tomuta
2 changed files with 18 additions and 2 deletions
|
@ -48,8 +48,15 @@ int sem_getvalue(sem_t*, int*)
|
|||
|
||||
int sem_init(sem_t* sem, int shared, unsigned int value)
|
||||
{
|
||||
if (shared)
|
||||
return ENOSYS;
|
||||
if (shared) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (value > SEM_VALUE_MAX) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pthread_mutex_init(&sem->mtx, nullptr) != 0)
|
||||
return -1;
|
||||
|
@ -70,6 +77,12 @@ sem_t* sem_open(const char*, int, ...)
|
|||
|
||||
int sem_post(sem_t* sem)
|
||||
{
|
||||
if (sem->value == SEM_VALUE_MAX) {
|
||||
pthread_mutex_unlock(&sem->mtx);
|
||||
errno = EOVERFLOW;
|
||||
return -1;
|
||||
}
|
||||
|
||||
sem->value++;
|
||||
|
||||
pthread_cond_signal(&sem->cv);
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
|
@ -48,4 +49,6 @@ int sem_trywait(sem_t*);
|
|||
int sem_unlink(const char*);
|
||||
int sem_wait(sem_t*);
|
||||
|
||||
#define SEM_VALUE_MAX INT_MAX
|
||||
|
||||
__END_DECLS
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue