Fixed some memory leaks
[pspp] / src / data / ods-reader.c
index 2b830fa2e6a8bf066d6ce1a0c7c1091aa06b96cb..cb16dbc6944187ea0312fe82f135c85623ea02df 100644 (file)
@@ -523,6 +523,8 @@ init_reader (struct ods_reader *r, bool report_errors)
 
   zip_member_ref (content);
 
+  if (r->xtr)
+    xmlFreeTextReader (r->xtr);
 
   xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
                        (xmlInputCloseCallback) zip_member_finish,
@@ -534,6 +536,10 @@ init_reader (struct ods_reader *r, bool report_errors)
 
   r->xtr = xtr;
   r->spreadsheet.type = SPREADSHEET_ODS;
+  r->row = 0;
+  r->col = 0;
+  r->current_sheet = 0;
+  r->state = STATE_INIT;
 
   if (report_errors) 
     xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
@@ -693,7 +699,7 @@ ods_make_reader (struct spreadsheet *spreadsheet,
                  /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
                  memset (var_spec + n_var_specs,
                          0, 
-                         (n_var_specs - idx + 1) * sizeof (*var_spec));
+                         (idx - n_var_specs + 1) * sizeof (*var_spec));
                  n_var_specs = idx + 1;
                }
              var_spec[idx].firstval.text = 0;
@@ -749,6 +755,10 @@ ods_make_reader (struct spreadsheet *spreadsheet,
          if (idx >= n_var_specs)
            {
              var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
+             memset (var_spec + n_var_specs,
+                     0, 
+                     (idx - n_var_specs + 1) * sizeof (*var_spec));
+
              var_spec [idx].name = NULL;
              n_var_specs = idx + 1;
            }
@@ -926,7 +936,7 @@ ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
          for (col = 0; col < r->col_span; ++col)
            {
              const struct variable *var;
-             const int idx = r->col + col - r->start_col - 1;
+             const int idx = r->col - col - r->start_col - 1;
              if (idx < 0)
                continue;
              if (r->stop_col != -1 && idx > r->stop_col - r->start_col )