From dfebf3174c20ff4efb254a84bb903faeb27135fe Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 11 Jan 2023 21:42:47 -1000 Subject: [PATCH] imp: csv: check assigned account names are valid (parseable) (#1978) --- hledger-lib/Hledger/Read/CsvReader.hs | 9 +++++++-- hledger/test/csv.test | 11 ++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index 80b286863..a3dbd5bce 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -73,7 +73,7 @@ import Text.Printf (printf) import Hledger.Data import Hledger.Utils -import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), amountp, statusp, journalFinalise ) +import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), amountp, statusp, journalFinalise, accountnamep ) --- ** doctest setup -- $setup @@ -1199,7 +1199,12 @@ getAccount rules record mamount mbalance n = -- accountN is set to the empty string - no posting will be generated Just "" -> Nothing -- accountN is set (possibly to "expenses:unknown"! #1192) - mark it final - Just a -> Just (a, True) + Just a -> + -- Check it and reject if invalid.. sometimes people try + -- to set an amount or comment along with the account name. + case parsewith (accountnamep >> eof) a of + Left e -> usageError $ errorBundlePretty e + Right _ -> Just (a, True) -- accountN is unset Nothing -> case (mamount, mbalance) of diff --git a/hledger/test/csv.test b/hledger/test/csv.test index 164920ab8..2c134bb5e 100644 --- a/hledger/test/csv.test +++ b/hledger/test/csv.test @@ -1059,7 +1059,16 @@ fields date, b, c $ ./csvtest.sh >= - +# 54. Some validation is done on account name assignments; trying to +# also set an amount there (with 2+ spaces) will be rejected. (#1978) +< +2022-01-01,1 +RULES +fields date,amt +account1 assets %amt +$ ./csvtest.sh +>2 /unexpected space/ +>=1 ## . #<