From 88f1d611ba3bdb895e7619bf7ab341e4756cd419 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Thu, 21 Apr 2016 12:39:36 +0200 Subject: [PATCH] Fix bug in syntax parser of GET DATA /TYPE=TXT Fixed a bug which manifested itself when GET DATA /TYPE=TXT was passed a variable name which happened also to be a token identifier. The actual bug might also have caused other ill effects, but seems to have been masked. Reported-by: Rob L --- src/language/data-io/get-data.c | 20 +++++++------- src/language/lexer/format-parser.c | 2 +- tests/language/data-io/get-data-txt.at | 37 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/language/data-io/get-data.c b/src/language/data-io/get-data.c index e3a93b1738..fd1e68de88 100644 --- a/src/language/data-io/get-data.c +++ b/src/language/data-io/get-data.c @@ -617,18 +617,20 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) lex_get (lexer); } - if (!lex_force_id (lexer) - || !dict_id_is_valid (dict, lex_tokcstr (lexer), true)) - goto error; name = xstrdup (lex_tokcstr (lexer)); + if (!lex_force_id (lexer) + || !dict_id_is_valid (dict, name, true)) + { + goto error; + } lex_get (lexer); - if (type == DP_DELIMITED) { if (!parse_format_specifier (lexer, &input) - || !fmt_check_input (&input)) - goto error; - + || !fmt_check_input (&input)) + { + goto error; + } output = fmt_for_output_from_input (&input); } else @@ -648,14 +650,12 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) msg (SE, _("Unknown format type `%s'."), fmt_type_name); goto error; } - /* Compose input format. */ input.type = fmt_type; input.w = lc - fc + 1; input.d = 0; if (!fmt_check_input (&input)) goto error; - /* Compose output format. */ if (w != 0) { @@ -668,7 +668,6 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) else output = fmt_for_output_from_input (&input); } - v = dict_create_var (dict, name, fmt_var_width (&input)); if (v == NULL) { @@ -676,7 +675,6 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds) goto error; } var_set_both_formats (v, &output); - if (type == DP_DELIMITED) data_parser_add_delimited_field (parser, &input, var_get_case_index (v), diff --git a/src/language/lexer/format-parser.c b/src/language/lexer/format-parser.c index 78af09da31..2c05335ff9 100644 --- a/src/language/lexer/format-parser.c +++ b/src/language/lexer/format-parser.c @@ -40,7 +40,7 @@ parse_abstract_format_specifier__ (struct lexer *lexer, struct substring type_ss, width_ss, decimals_ss; bool has_decimals; - if (lex_token (lexer) != T_ID) + if (lex_token (lexer) != T_ID && lex_token (lexer) != T_STRING) goto error; /* Extract pieces. */ diff --git a/tests/language/data-io/get-data-txt.at b/tests/language/data-io/get-data-txt.at index b0b2616f7a..53f5d4fddc 100644 --- a/tests/language/data-io/get-data-txt.at +++ b/tests/language/data-io/get-data-txt.at @@ -343,3 +343,40 @@ AT_CHECK([pspp -o pspp.csv x.sps], [1], [ignore]) AT_CLEANUP + +AT_SETUP([GET DATA /TYPE=txt bug]) + + +AT_DATA([thing.txt], [dnl +foo, title, last +1, this, 1 +2, that, 2 +3, other, 3 +]) + +AT_DATA([x.sps], [dnl +GET DATA + /TYPE=TXT + /FILE="thing.txt" + /ARRANGEMENT=DELIMITED + /DELCASE=LINE + /FIRSTCASE=2 + /DELIMITERS="," + /VARIABLES=foo F1.0 + title A8 + last F2.0. + +list. +]) + +AT_CHECK([pspp -O format=csv x.sps], [0], [dnl +Table: Data List +foo,title,last +1,this ,1 +2,that ,2 +3,other ,3 +]) + +AT_CLEANUP + + -- 2.30.2