From a1ec2113e917faa9ba0d6d9bd40d1694a588d17a Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Fri, 4 Jan 2013 10:11:32 +0000 Subject: [PATCH] Fix sort_scores in the acro plugin - pass the correct number of array entries to qsort() and properly reconstruct the list. git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@224 13b04d17-f746-0410-82c6-800466cd88b0 --- Changelog | 2 +- dll/acro/acro.c | 66 +++++++++++++++++++++++++++++-------------------- dll/acro/acro.h | 1 - 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/Changelog b/Changelog index 9002f0d..bf0afff 100644 --- a/Changelog +++ b/Changelog @@ -1,6 +1,6 @@ [Changes 1.2c01] -* Fix timers in acro plugin, clears up some build warnings. (caf) +* Clears up a crash and some build warnings in the acro plugin. (caf) * Applied patches from jdhore to clean up the plugin building and installing process. (caf) diff --git a/dll/acro/acro.c b/dll/acro/acro.c index 38a50e2..af1123e 100644 --- a/dll/acro/acro.c +++ b/dll/acro/acro.c @@ -448,40 +448,17 @@ srec *end_vote(vrec *voters, prec *players, srec *stmp) return stmp; } -srec *sort_scores(srec *stmp) -{ - int i = 0; - srec *tmp; - srec **sort, **ctmp; - if (!stmp->next) - return stmp; - for (tmp = stmp; tmp; tmp = tmp->next) - i++; - ctmp = sort = (srec **)new_malloc(i*sizeof(srec *)); - put_it("START SORTING"); - put_scores(NULL, NULL, NULL, NULL, NULL); - for (tmp = stmp; tmp; tmp = tmp->next) - *ctmp++ = tmp; - qsort((void *)sort, i+1, sizeof(srec *), (int (*)(const void *, const void *))comp_score); - ctmp = sort; - for (tmp = *ctmp++; *ctmp; ctmp++) - tmp = tmp->next = *ctmp; - tmp->next = NULL; - tmp = *sort; - new_free(&sort); - put_scores(NULL, NULL, NULL, NULL, NULL); - put_it("END SCORES"); - return tmp; -} - /* * Here we sort deeze babys ... The return values are "opposite" so we can * sort in descending order instead of ascending... Stupid declarations had * me going for a while, no wonder it didnt sort right at first! :) */ -int comp_score(srec **one, srec **two) +static int comp_score(const void *a, const void *b) { + srec * const *one = a; + srec * const *two = b; + if ((*one)->score > (*two)->score) return -1; if ((*one)->score < (*two)->score) @@ -490,6 +467,41 @@ int comp_score(srec **one, srec **two) return strcasecmp((*one)->nick, (*two)->nick); } +srec *sort_scores(srec *stmp) +{ + size_t n = 0; + srec *tmp; + srec **sort, **ctmp; + + if (!stmp->next) + return stmp; + + for (tmp = stmp; tmp; tmp = tmp->next) + n++; + ctmp = sort = (srec **)new_malloc(n * sizeof sort[0]); + + put_it("START SORTING"); + put_scores(NULL, NULL, NULL, NULL, NULL); + + for (tmp = stmp; tmp; tmp = tmp->next) + *ctmp++ = tmp; + qsort(sort, n, sizeof sort[0], comp_score); + + tmp = sort[0]; + for (ctmp = &sort[1]; ctmp < &sort[n]; ctmp++) + { + tmp->next = *ctmp; + tmp = *ctmp; + } + tmp->next = NULL; + + tmp = sort[0]; + new_free(&sort); + put_scores(NULL, NULL, NULL, NULL, NULL); + put_it("END SCORES"); + return tmp; +} + void show_acros(prec *players, char *chan) { prec *tmp; diff --git a/dll/acro/acro.h b/dll/acro/acro.h index 87a4696..9e5af93 100644 --- a/dll/acro/acro.h +++ b/dll/acro/acro.h @@ -104,7 +104,6 @@ prec *take_acro(grec *, prec *, char *, char *, char *); vrec *take_vote(grec *, vrec *, prec *, char *, char *, char *); srec *end_vote(vrec *, prec *, srec *); srec *sort_scores(srec *); -int comp_score(srec **one, srec **two); void show_scores(grec *, srec *, srec *, char *); int warn_acro(void *, char *); int start_vote(void *, char *);