str: Skip only up to one-past-the-end in ss_tokenize().
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 18 Feb 2012 19:03:01 +0000 (11:03 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 18 Feb 2012 19:03:23 +0000 (11:03 -0800)
If ss_tokenize() was called with '*save_idx' positioned at
one-past-the-end of a string, it would continue to advance it past
that point, which is surprising.  This avoids doing that.

Found by Valgrind.
Reported-by: John Darrington <john@darrington.wattle.id.au>
src/libpspp/str.c
tests/data/sys-file-reader.at

index 7e722c17e6b92602798725e3bdd618327a46691f..79e9ea1e14dae1def667b3f4edd1ea60f4e0ac5d 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
@@ -491,11 +491,15 @@ bool
 ss_tokenize (struct substring ss, struct substring delimiters,
              size_t *save_idx, struct substring *token)
 {
+  bool found_token;
+
   ss_advance (&ss, *save_idx);
   *save_idx += ss_ltrim (&ss, delimiters);
   ss_get_bytes (&ss, ss_cspan (ss, delimiters), token);
-  *save_idx += ss_length (*token) + 1;
-  return ss_length (*token) > 0;
+
+  found_token = ss_length (*token) > 0;
+  *save_idx += ss_length (*token) + found_token;
+  return found_token;
 }
 
 /* Removes the first CNT bytes from SS. */
index cdf6366e94fe4baaf6bf153e4a5a92a78b70cbe5..37866161d9902eef4cd0f8e68de9ce9547bebbef 100644 (file)
@@ -2354,7 +2354,7 @@ do
   AT_DATA([sys-file.sps], [GET FILE='sys-file.sav'.
 ])
   AT_CHECK([pspp -O format=csv sys-file.sps], [0], [dnl
-warning: `sys-file.sav' near offset 0xd8: Missing new-line parsing variable names at offset 14 in MRSETS record.
+warning: `sys-file.sav' near offset 0xd8: Missing new-line parsing variable names at offset 13 in MRSETS record.
 
 warning: `sys-file.sav' near offset 0xd8: MRSET $a has only 1 variables.
 ])
@@ -2986,9 +2986,9 @@ do
 GET FILE='sys-file.sav'.
 ])
   AT_CHECK([pspp -O format=csv sys-file.sps], [0], [dnl
-warning: `sys-file.sav' near offset 0xdf: Error parsing attribute value Attr1[[1]].
+warning: `sys-file.sav' near offset 0xde: Error parsing attribute value Attr1[[1]].
 
-warning: `sys-file.sav' near offset 0x102: Error parsing attribute value fred[[2]].
+warning: `sys-file.sav' near offset 0x101: Error parsing attribute value fred[[2]].
 ])
 done
 AT_CLEANUP