por-file-writer: Avoid infinite loop with value labels. 20120207030502/pspp
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 7 Feb 2012 06:23:44 +0000 (22:23 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 7 Feb 2012 06:25:47 +0000 (22:25 -0800)
Bug report and fix by Kent Nassen <knassen@umich.edu>.

src/data/por-file-writer.c
tests/data/por-file.at

index 3c475d30b58d83b9e0217e88d8d22d05afe4383b..39c763113aad1343d448ab06566857b9d0edcaec 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -404,6 +404,7 @@ write_value_labels (struct pfm_writer *w, const struct dictionary *dict)
       const struct val_labs *val_labs = var_get_value_labels (v);
       size_t n_labels = val_labs_count (val_labs);
       const struct val_lab **labels;
+      int j;
 
       if (n_labels == 0)
        continue;
@@ -415,9 +416,9 @@ write_value_labels (struct pfm_writer *w, const struct dictionary *dict)
 
       n_labels = val_labs_count (val_labs);
       labels = val_labs_sorted (val_labs);
-      for (i = 0; i < n_labels; i++)
+      for (j = 0; j < n_labels; j++)
         {
-          const struct val_lab *vl = labels[i];
+          const struct val_lab *vl = labels[j];
           write_value (w, val_lab_get_value (vl), var_get_width (v));
           write_string (w, val_lab_get_escaped_label (vl));
         }
index e3ddab4bec7d33e3852f10ca5fdde4b8ed991ca3..2020f0a03446ccede4530c97f0477963e0993ac7 100644 (file)
@@ -63,3 +63,60 @@ X,Y
 2,4.00
 ])
 AT_CLEANUP
+
+dnl This checks for a bug in the portable file writer that caused an infinite
+dnl loop in some cases, and other problems in other cases, when a variable
+dnl had value labels.
+AT_SETUP([write portable file with value labels])
+AT_DATA([export.sps], [dnl
+DATA LIST NOTABLE/var1 TO var5 1-5.
+VALUE LABELS
+       /var1 1 'one'
+       /var2 2 'two'
+       /var3 3 'three'
+       /var4 4 'four'
+       /var5 5 'five'.
+BEGIN DATA.
+12345
+END DATA.
+EXPORT OUTFILE='data.por'.
+])
+AT_CHECK([pspp -O format=csv export.sps])
+AT_DATA([import.sps], [dnl
+IMPORT FILE='data.por'.
+DISPLAY DICTIONARY.
+LIST.
+])
+AT_CHECK([pspp -O format=csv import.sps], [0], [dnl
+Variable,Description,,Position
+VAR1,Format: F1.0,,1
+,Measure: Scale,,
+,Display Alignment: Right,,
+,Display Width: 8,,
+,1,one,
+VAR2,Format: F1.0,,2
+,Measure: Scale,,
+,Display Alignment: Right,,
+,Display Width: 8,,
+,2,two,
+VAR3,Format: F1.0,,3
+,Measure: Scale,,
+,Display Alignment: Right,,
+,Display Width: 8,,
+,3,three,
+VAR4,Format: F1.0,,4
+,Measure: Scale,,
+,Display Alignment: Right,,
+,Display Width: 8,,
+,4,four,
+VAR5,Format: F1.0,,5
+,Measure: Scale,,
+,Display Alignment: Right,,
+,Display Width: 8,,
+,5,five,
+
+Table: Data List
+VAR1,VAR2,VAR3,VAR4,VAR5
+1,2,3,4,5
+])
+AT_CLEANUP