From: Yu Watanabe Date: Wed, 26 Apr 2023 05:37:26 +0000 (+0900) Subject: core/job: handle job ID overflow or conflict more sanely X-Git-Tag: v254-rc1~612^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3e1aec303646c37e7a7c74b45ef8cc90e3bf3531;p=thirdparty%2Fsystemd.git core/job: handle job ID overflow or conflict more sanely This is paranoia, and just for safety. Should not change any behavior. --- diff --git a/src/core/job.c b/src/core/job.c index 1010e6f869c..43a06a365e3 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -48,6 +48,28 @@ Job* job_new_raw(Unit *unit) { return j; } +static uint32_t manager_get_new_job_id(Manager *m) { + bool overflow = false; + + assert(m); + + for (;;) { + uint32_t id = m->current_job_id; + + if (_unlikely_(id == UINT32_MAX)) { + assert_se(!overflow); + m->current_job_id = 1; + overflow = true; + } else + m->current_job_id++; + + if (hashmap_contains(m->jobs, UINT32_TO_PTR(id))) + continue; + + return id; + } +} + Job* job_new(Unit *unit, JobType type) { Job *j; @@ -57,7 +79,7 @@ Job* job_new(Unit *unit, JobType type) { if (!j) return NULL; - j->id = j->manager->current_job_id++; + j->id = manager_get_new_job_id(j->manager); j->type = type; /* We don't link it here, that's what job_dependency() is for */