From: Ben Pfaff Date: Sat, 18 Feb 2012 19:03:01 +0000 (-0800) Subject: str: Skip only up to one-past-the-end in ss_tokenize(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;ds=sidebyside;h=c60b1277e785531b1cfc26b48697af7f942561d3;p=pspp str: Skip only up to one-past-the-end in ss_tokenize(). 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 --- diff --git a/src/libpspp/str.c b/src/libpspp/str.c index 7e722c17e6..79e9ea1e14 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -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. */ diff --git a/tests/data/sys-file-reader.at b/tests/data/sys-file-reader.at index cdf6366e94..37866161d9 100644 --- a/tests/data/sys-file-reader.at +++ b/tests/data/sys-file-reader.at @@ -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