From 08b6dd3f7a24ac94ffd5ae2541bccacb3048f757 Mon Sep 17 00:00:00 2001 From: Dennis Korpel Date: Tue, 2 Apr 2024 15:46:44 +0200 Subject: [PATCH] Fix bugzilla 24478 - std.csv array out of bounds when row size exceeds header --- std/csv.d | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/std/csv.d b/std/csv.d index 7f5c2b24c..9ee9b5d5b 100644 --- a/std/csv.d +++ b/std/csv.d @@ -175,6 +175,16 @@ class CSVException : Exception assert(ex.toString == "(Row: 1, Col: 2) Unexpected 'b' when converting from type string to type int"); } +// https://issues.dlang.org/show_bug.cgi?id=24478 +@safe unittest +{ + import std.exception : collectException; + import std.algorithm.searching : count; + string text = "A, B\n1, 2, 3"; + auto ex = collectException!CSVException(csvReader!(string[string])(text, null).count); + assert(ex.toString == "(Row: 1, Col: 3) row contains more values than header"); +} + @safe pure unittest { import std.string; @@ -1179,7 +1189,10 @@ public: { for (; !recordRange.empty; recordRange.popFront()) { - aa[header[_input.col-1]] = recordRange.front; + const i = _input.col - 1; + if (i >= header.length) + throw new CSVException("row contains more values than header", _input.row, _input.col); + aa[header[i]] = recordRange.front; } } catch (ConvException e)