From 4ad92b9884dda1556636170cc5b03d281eb6eb2f Mon Sep 17 00:00:00 2001 From: Tom Stellard Date: Fri, 19 Feb 2021 08:37:03 -0800 Subject: [PATCH] Avoid infinite loop in job_pick_team_from_country() This function was looping forever if all the national teams were added to the job list. --- src/job.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/job.c b/src/job.c index a81c7499..17256452 100644 --- a/src/job.c +++ b/src/job.c @@ -50,6 +50,7 @@ job_update(Bygfoot *bygfoot) gint i; gint new_offers, int_offers; + gint national_teams = 0; for(i=jobs->len - 1; i >= 0; i--) { @@ -62,8 +63,19 @@ job_update(Bygfoot *bygfoot) if(week % const_int("int_job_update_interval") != 2) return; + for (i = 0; i < country.leagues->len; i++) { + national_teams += g_array_index(country.leagues, League, i).teams->len; + } + + /* Limit the total number of jobs to the number of national teams. + * Otherwise, we risk hitting an infinite loop in + * job_pick_team_from_country(); + */ + new_offers = math_rndi(const_int("int_job_new_offers_lower"), const_int("int_job_new_offers_upper")); + + new_offers = MIN(new_offers, national_teams - jobs->len); int_offers = (users->len == 1) ? (gint)rint((gfloat)new_offers * const_float("float_job_international_perc")) : 0;