From f2828f801736701c0294803b5dedd4c4ab63b45e Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 4 Mar 2004 04:56:50 +0000 Subject: [PATCH] Fix a lot of "possibly uninitialized variable" warnings. Some of them were real bugs. A few of them make me wonder how the code ever worked. --- src/ChangeLog | 60 ++++++++++++++++++++++++++++++++++++++++++++++ src/aggregate.c | 1 + src/ascii.c | 2 ++ src/crosstabs.q | 25 +++++++------------ src/data-in.c | 2 ++ src/data-list.c | 10 ++++---- src/expr-opt.c | 5 ++-- src/frequencies.q | 9 ++++--- src/groff-font.c | 2 +- src/matrix-data.c | 3 ++- src/output.c | 1 + src/pool.c | 2 ++ src/postscript.c | 24 ++++++++++--------- src/print.c | 4 ++-- src/recode.c | 10 +++++++- src/sfm-read.c | 2 +- src/sysfile-info.c | 6 ++--- src/vars-prs.c | 2 +- 18 files changed, 121 insertions(+), 49 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 57862a16..0dcff3dc 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,63 @@ +Wed Mar 3 20:44:37 2004 Ben Pfaff + + Fix a lot of "possibly uninitialized variable" warnings. Some of + them are even real bugs. A few of them make me wonder how the + code ever worked. + + * aggregate.c: (parse_aggregate_functions) Initialize `function. + + * ascii.c: (output_lines) Add default case to switch. + + * crosstabs.q: Remove static variable `expected' and all + references to it. + (display_crosstabulation) Always calculate expected value. + (calc_chisq) Ditto. + (output_pivot_table) Initialize `cmp'. + (display_crosstabulation) New variable `last_row', which is + initialized. + + * data-in.c: (parse_numeric) Always initialize sign. How did this + work at all?! + + * data-list.c: (repeating_data_trns_proc) Always initialize code. + Always set info.ofs. (How did this work?!) + + * expr-opt.c: (optimize_tree) Always initialize `m'. + (evaluate_tree) Always initialize `c'. (How did this work?) + + * frequencies.q: (frq_custom_variables) Always initialize min, + max. + (frq_custom_grouped) Always initialize `dl'. + + * groff-font.c: (groff_read_font) Always initialize char_set. + + * matrix-data.c: (nr_output_data) Initialize `split'. + (wr_read_splits) Remove shadowing split_cnt declaration. + (wr_output_data) Initialize `split'. + + * output.c: (tokener) Skip add character on syntax error. + + * pool.c: (pool_strndup) Always set `copy'. (How did this work?!) + + * postscript.c: (read_ps_encodings) Use line.string instead of + uninitialized `bp'. + (write_text) Add default case to switch. + (text) Always initialize multiple variables. Fix bug with + ligatures. + + * print.c: (fixed_parse_fortran) Initialize head. + (alloc_line) Add default case to switch. + + * recode.c: (parse_dest_spec) Handle case where nothing matches. + (recode_trns_proc) Move variable declaration inward. Add default + case to switch. + + * sfm-read.c: (read_header) Initialize skip_amt. + + * sysfile-info.c: (display_variables) Always initialize pc. + + * vars-prs.c: Initialized `included'. + Wed Mar 3 09:30:09 2004 Ben Pfaff * main.c: (main) sigaction()'s sa_flags member was uninitialized. diff --git a/src/aggregate.c b/src/aggregate.c index e8568419..880a713a 100644 --- a/src/aggregate.c +++ b/src/aggregate.c @@ -458,6 +458,7 @@ parse_aggregate_functions (void) dest_label = NULL; n_dest = 0; src = NULL; + function = NULL; n_src = 0; arg[0].c = NULL; arg[1].c = NULL; diff --git a/src/ascii.c b/src/ascii.c index b9222b83..d1818aad 100644 --- a/src/ascii.c +++ b/src/ascii.c @@ -1461,6 +1461,8 @@ output_lines (struct outp_driver *this, int first, int count) ch = *ep; n_passes = 3; break; + default: + assert (0); } output_char (this, 1, ch); n_chars += ep - bp + 1; diff --git a/src/crosstabs.q b/src/crosstabs.q index 36c388ac..d880ceb0 100644 --- a/src/crosstabs.q +++ b/src/crosstabs.q @@ -135,7 +135,6 @@ static int mode; /* CELLS. */ static int num_cells; /* Number of cells requested. */ static int cells[8]; /* Cells requested. */ -static int expected; /* Nonzero if expected value is needed. */ /* WRITE. */ static int write; /* One of WR_* that specifies the WRITE style. */ @@ -198,7 +197,6 @@ internal_cmd_crosstabs (void) mode = variables ? INTEGER : GENERAL; /* CELLS. */ - expected = 0; if (!cmd.sbc_cells) { cmd.a_cells[CRS_CL_COUNT] = 1; @@ -228,11 +226,7 @@ internal_cmd_crosstabs (void) cmd.a_cells[CRS_CL_NONE] = 0; for (num_cells = i = 0; i < CRS_CL_count; i++) if (cmd.a_cells[i]) - { - if (i >= CRS_CL_EXPECTED) - expected = 1; - cmd.a_cells[num_cells++] = i; - } + cmd.a_cells[num_cells++] = i; } /* STATISTICS. */ @@ -1036,7 +1030,7 @@ output_pivot_table (struct table_entry **pb, struct table_entry **pe, int tc = pe - pb; /* Table count. */ /* Table entry for header comparison. */ - struct table_entry *cmp; + struct table_entry *cmp = NULL; x = xtab[(*pb)->table]; enum_var_values (pb, pe - pb, COL_VAR, &cols, &n_cols); @@ -1817,10 +1811,7 @@ display_crosstabulation (void) tab_hline (table, TAL_1, -1, n_cols, 0); for (c = 0; c < n_cols; c++) { - double expected_value; - - if (expected) - expected_value = row_tot[r] * col_tot[c] / W; + double expected_value = row_tot[r] * col_tot[c] / W; for (i = 0; i < num_cells; i++) { double v; @@ -1920,6 +1911,7 @@ display_crosstabulation (void) /* Column totals, grand total. */ { int c, j; + int last_row = 0; if (num_cells > 1) tab_hline (table, TAL_1, -1, n_cols, 0); @@ -1963,9 +1955,10 @@ display_crosstabulation (void) j++; } + last_row = j; } - tab_offset (table, -1, tab_row (table) + j); + tab_offset (table, -1, tab_row (table) + last_row); } tab_offset (table, 0, -1); @@ -2368,8 +2361,7 @@ calc_chisq (double chisq[N_CHISQ], int df[N_CHISQ], const double freq = mat[n_cols * r + c]; const double residual = freq - expected; - if (expected) - chisq[0] += residual * residual / expected; + chisq[0] += residual * residual / expected; if (freq) chisq[1] += freq * log (expected / freq); } @@ -2542,8 +2534,7 @@ calc_symmetric (double v[N_SYMMETRIC], double ase[N_SYMMETRIC], const double freq = mat[n_cols * r + c]; const double residual = freq - expected; - if (expected) - Xp += residual * residual / expected; + Xp += residual * residual / expected; } } diff --git a/src/data-in.c b/src/data-in.c index 586e74f3..3a15f896 100644 --- a/src/data-in.c +++ b/src/data-in.c @@ -140,6 +140,8 @@ parse_numeric (struct data_in *i) if (*i->s == '-' || *i->s == '+') i->s++; } + else + sign = 1; if (type != FMT_DOT) { diff --git a/src/data-list.c b/src/data-list.c index d987a2f7..11b7f9c7 100644 --- a/src/data-list.c +++ b/src/data-list.c @@ -1917,26 +1917,27 @@ repeating_data_trns_proc (struct trns_header *trns, struct ccase *c, info.len = len; info.beg = starts_beg; info.end = starts_end; + info.ofs = length; info.c = c; info.verify_id = 0; info.max_occurs = occurs_left; - code = rpd_parse_record (&info);; + code = rpd_parse_record (&info); if (!code) return -2; + occurs_left -= code; } else if (cont_beg == 0) return -3; /* Make sure, if some occurrences are left, that we have continuation records. */ - occurs_left -= code; - if (occurs_left != 0 && cont_beg == 0) + if (occurs_left > 0 && cont_beg == 0) { tmsg (SE, RPD_ERR, _("Number of repetitions specified on OCCURS (%d) " "exceed number of repetitions available in " "space on STARTS (%d), and CONTINUED not specified."), - occurs, code); + occurs, (starts_end - starts_beg + 1) / length); return -2; } @@ -1965,6 +1966,7 @@ repeating_data_trns_proc (struct trns_header *trns, struct ccase *c, info.len = len; info.beg = cont_beg; info.end = cont_end; + info.ofs = length; info.c = c; info.verify_id = 1; info.max_occurs = occurs_left; diff --git a/src/expr-opt.c b/src/expr-opt.c index 0a0de2a7..4cfaf04b 100644 --- a/src/expr-opt.c +++ b/src/expr-opt.c @@ -145,7 +145,7 @@ optimize_tree (struct nonterm_node * n) int nvar = 0; /* New node. */ - struct nonterm_node *m; + struct nonterm_node *m = NULL; /* Argument copying counter. */ int c; @@ -768,7 +768,7 @@ evaluate_tree (struct nonterm_node * n) case OP_RTRIM: case OP_RTRIM_OPT: { - int c; + int c = ' '; char *cp = s0; if (n->type == OP_LTRIM_OPT || n->type == OP_RTRIM_OPT) @@ -778,7 +778,6 @@ evaluate_tree (struct nonterm_node * n) c = n->type == OP_LTRIM_OPT ? 'L' : 'R'; msg (SE, _("Second argument to %cTRIM() must be at least one " "character in length."), c); - c = ' '; } else c = s1[0]; diff --git a/src/frequencies.q b/src/frequencies.q index 0d896b47..d9f8660f 100644 --- a/src/frequencies.q +++ b/src/frequencies.q @@ -589,7 +589,7 @@ static int frq_custom_variables (struct cmd_frequencies *cmd UNUSED) { int mode; - int min, max; + int min = 0, max = 0; int old_n_variables = n_variables; int i; @@ -716,8 +716,11 @@ frq_custom_grouped (struct cmd_frequencies *cmd UNUSED) return 0; } } - else - nl = 0; + else + { + nl = 0; + dl = NULL; + } for (i = 0; i < n; i++) { diff --git a/src/groff-font.c b/src/groff-font.c index e4df2cff..15135aa1 100644 --- a/src/groff-font.c +++ b/src/groff-font.c @@ -92,7 +92,7 @@ groff_read_font (const char *fn) char *key; /* 0=kernpairs section, 1=charset section. */ - int charset; + int charset = 0; /* Index for previous line. */ int prev_index = -1; diff --git a/src/matrix-data.c b/src/matrix-data.c index 1532d1a1..b47112a1 100644 --- a/src/matrix-data.c +++ b/src/matrix-data.c @@ -1452,6 +1452,7 @@ nr_output_data (struct matrix_data_pgm *mx, size_t i; split_cnt = dict_get_split_cnt (default_dict); + split = dict_get_split_vars (default_dict); for (i = 0; i < split_cnt; i++) temp_case->data[split[i]->fv].f = split_values[i]; } @@ -1600,7 +1601,6 @@ wr_read_splits (struct matrix_data_pgm *mx, { int different = 0; - size_t split_cnt; int i; for (i = 0; i < split_cnt; i++) @@ -1677,6 +1677,7 @@ wr_output_data (struct matrix_data_pgm *mx, size_t i; split_cnt = dict_get_split_cnt (default_dict); + split = dict_get_split_vars (default_dict); for (i = 0; i < split_cnt; i++) temp_case->data[split[i]->fv].f = split_values[i]; } diff --git a/src/output.c b/src/output.c index 6a4ed78d..64c1e8e8 100644 --- a/src/output.c +++ b/src/output.c @@ -588,6 +588,7 @@ tokener (void) break; default: msg (IS, _("Syntax error in string constant.")); + continue; } ds_putchar (&op_tokstr, (unsigned char) c); } diff --git a/src/pool.c b/src/pool.c index 336671e2..0123a8b9 100644 --- a/src/pool.c +++ b/src/pool.c @@ -302,6 +302,8 @@ pool_strndup (struct pool *pool, const char *string, size_t length) copy = ((char *) b) + b->ofs; b->ofs += size; } + else + copy = pool_alloc (pool, size); } #else copy = pool_alloc (pool, size); diff --git a/src/postscript.c b/src/postscript.c index 8a11935a..b911f8cc 100644 --- a/src/postscript.c +++ b/src/postscript.c @@ -1106,8 +1106,6 @@ read_ps_encodings (struct outp_driver *this) for (;;) { - char *bp; - if (!ds_get_config_line (f, &line, &where)) { if (ferror (f)) @@ -1115,7 +1113,7 @@ read_ps_encodings (struct outp_driver *this) break; } - add_encoding (this, bp); + add_encoding (this, line.string); } ds_destroy (&line); @@ -2410,6 +2408,8 @@ write_text (struct outp_driver *this, case OUTP_T_JUST_CENTER: ofs = width_left / 2; break; + default: + assert (0); } lp = line; @@ -2547,11 +2547,16 @@ text (struct outp_driver *this, struct outp_text *t, int draw) x = t->x; y = t->y; } + else + x = y = 0; width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX; height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX; max_height = 0; prev_char = -1; space_char = NULL; + space_buf_loc = NULL; + space_width_left = 0; + if (!width || !height_left) goto exit; @@ -2566,9 +2571,12 @@ text (struct outp_driver *this, struct outp_text *t, int draw) /* Set char_name to the name of the character or ligature at *cp. */ + local_char_name[0] = *cp; + char_name = local_char_name; if (ext->current->font->ligatures && *cp == 'f') { - int lig = 0; + int lig; + char_name = NULL; if (cp < end - 1) switch (cp[1]) @@ -2599,8 +2607,6 @@ text (struct outp_driver *this, struct outp_text *t, int draw) local_char_name[0] = *cp++; /* 'f' */ char_name = local_char_name; } - else - cp += strlen (char_name); } else if (*cp == '\n') { @@ -2623,11 +2629,7 @@ text (struct outp_driver *this, struct outp_text *t, int draw) set separate to 1. */ continue; } - else - { - local_char_name[0] = *cp++; - char_name = local_char_name; - } + cp += strlen (char_name); /* Figure out what size this character is, and what kern adjustment we need. */ diff --git a/src/print.c b/src/print.c index 9c4b63b2..95dae157 100644 --- a/src/print.c +++ b/src/print.c @@ -717,7 +717,7 @@ dump_fmt_list (struct fmt_list * f) static struct fmt_list * fixed_parse_fortran (void) { - struct fmt_list *head; + struct fmt_list *head = NULL; struct fmt_list *fl = NULL; lex_get (); /* skip opening parenthesis */ @@ -886,8 +886,8 @@ alloc_line (void) pot_w = i->fc + 1; break; case PRT_ERROR: + default: assert (0); - break; } if (pot_w > w) w = pot_w; diff --git a/src/recode.c b/src/recode.c index 523dcaa6..53a8e23a 100644 --- a/src/recode.c +++ b/src/recode.c @@ -487,6 +487,11 @@ parse_dest_spec (struct rcd_var * rcd, union value * v, size_t *max_dst_width) v->c = NULL; } } + else + { + lex_error (_("expecting output value")); + return 0; + } if ((rcd->flags & RCD_DEST_MASK) == RCD_DEST_ERROR) rcd->flags |= flags; @@ -776,10 +781,11 @@ recode_trns_proc (struct trns_header * t, struct ccase * c, int case_num UNUSED) { struct rcd_var *v; - struct coding *cp; for (v = ((struct recode_trns *) t)->codings; v; v = v->next) { + struct coding *cp; + switch (v->flags & RCD_SRC_MASK) { case RCD_SRC_NUMERIC: @@ -788,6 +794,8 @@ recode_trns_proc (struct trns_header * t, struct ccase * c, case RCD_SRC_STRING: cp = find_src_string (v, c); break; + default: + assert (0); } if (!cp) continue; diff --git a/src/sfm-read.c b/src/sfm-read.c index c38aa1c2..c14e11ac 100644 --- a/src/sfm-read.c +++ b/src/sfm-read.c @@ -523,7 +523,7 @@ read_header (struct file_handle * h, struct sfm_read_info * inf) struct sysfile_header hdr; /* Disk buffer. */ struct dictionary *dict; /* File dictionary. */ char prod_name[sizeof hdr.prod_name + 1]; /* Buffer for product name. */ - int skip_amt; /* Amount of product name to omit. */ + int skip_amt = 0; /* Amount of product name to omit. */ int i; /* Create the dictionary. */ diff --git a/src/sysfile-info.c b/src/sysfile-info.c index dd33d574..f35bead7 100644 --- a/src/sysfile-info.c +++ b/src/sysfile-info.c @@ -368,11 +368,9 @@ display_variables (struct variable **vl, int n, int as) nr = n + 5; tab_hline (t, TAL_2, 0, nc - 1, 1); tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable")); + pc = (as == AS_INDEX ? 1 : 3); if (as != AS_NAMES) - { - pc = (as == AS_INDEX ? 1 : 3); - tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position")); - } + tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position")); if (as == AS_DICTIONARY || as == AS_VARIABLES) tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description")); else if (as == AS_LABELS) diff --git a/src/vars-prs.c b/src/vars-prs.c index 2377d538..66fec9ae 100644 --- a/src/vars-prs.c +++ b/src/vars-prs.c @@ -124,7 +124,7 @@ parse_var_set_vars (struct var_set *vs, { size_t vs_var_cnt; int i; - char *included; + char *included = NULL; struct variable *v1, *v2; int count, mv; -- 2.30.2