liquid_feedback_core

changeset 366:59598477298c

Fixed errors in function "process_initiative" of "lf_update_suggestion_order", which caused memory access errors
author jbe
date Sun Mar 17 15:26:30 2013 +0100 (2013-03-17)
parents 90ac85d184f8
children f690ef9147f7
files lf_update_suggestion_order.c
line diff
     1.1 --- a/lf_update_suggestion_order.c	Sun Mar 17 12:54:15 2013 +0100
     1.2 +++ b/lf_update_suggestion_order.c	Sun Mar 17 15:26:30 2013 +0100
     1.3 @@ -222,7 +222,7 @@
     1.4  
     1.5  static int process_initiative(PGconn *db, PGresult *res, char *escaped_initiative_id, int final) {
     1.6    int err;
     1.7 -  int ballot_count = 0;
     1.8 +  int ballot_count = 1;
     1.9    struct ballot *ballots;
    1.10    int i;
    1.11    {
    1.12 @@ -244,22 +244,18 @@
    1.13        }
    1.14      }
    1.15      candidate_count = 0;
    1.16 -    for (i=0; i<=tuple_count; i++) {
    1.17 +    for (i=0; i<tuple_count; i++) {
    1.18        char *member_id, *suggestion_id;
    1.19 -      if (i<tuple_count) {
    1.20 -        member_id = PQgetvalue(res, i, COL_MEMBER_ID);
    1.21 -        suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
    1.22 -        if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) {
    1.23 -          candidate_count++;
    1.24 -          if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) {
    1.25 -            fprintf(stderr, "Insufficient memory while inserting into candidate tree.\n");
    1.26 -            abort();
    1.27 -          }
    1.28 +      member_id = PQgetvalue(res, i, COL_MEMBER_ID);
    1.29 +      suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
    1.30 +      if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) {
    1.31 +        candidate_count++;
    1.32 +        if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) {
    1.33 +          fprintf(stderr, "Insufficient memory while inserting into candidate tree.\n");
    1.34 +          abort();
    1.35          }
    1.36        }
    1.37 -      if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
    1.38 -        ballot_count++;
    1.39 -      }
    1.40 +      if (old_member_id && strcmp(old_member_id, member_id)) ballot_count++;
    1.41        old_member_id = member_id;
    1.42      }
    1.43      printf("Candidate count: %i\n", candidate_count);
    1.44 @@ -298,9 +294,9 @@
    1.45          return 1;
    1.46        }
    1.47        preference--;
    1.48 +      if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
    1.49        ballot->weight = weight;
    1.50        ballot->sections[preference].count++;
    1.51 -      if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
    1.52        old_member_id = member_id;
    1.53      }
    1.54      for (i=0; i<ballot_count; i++) {
    1.55 @@ -320,14 +316,18 @@
    1.56      for (i=0; i<tuple_count; i++) {
    1.57        char *member_id, *suggestion_id;
    1.58        int preference;
    1.59 -      if (i<tuple_count) {
    1.60 -        member_id = PQgetvalue(res, i, COL_MEMBER_ID);
    1.61 -        suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
    1.62 -        preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
    1.63 -        preference--;
    1.64 -        ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
    1.65 +      member_id = PQgetvalue(res, i, COL_MEMBER_ID);
    1.66 +      suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
    1.67 +      preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
    1.68 +      preference--;
    1.69 +      if (old_member_id && strcmp(old_member_id, member_id)) {
    1.70 +        ballot++;
    1.71 +        candidates_in_sections[0] = 0;
    1.72 +        candidates_in_sections[1] = 0;
    1.73 +        candidates_in_sections[2] = 0;
    1.74 +        candidates_in_sections[3] = 0;
    1.75        }
    1.76 -      if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
    1.77 +      ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
    1.78        old_member_id = member_id;
    1.79      }
    1.80    }

Impressum / About Us