Summary
ParseISOTime accepted the HH:MM format pattern but crashed when parsing because it always tried to read seconds from position 7.
Bug Description
The function pattern matching allowed ##:## format:
if not MatchPattern('##:##:##:###', str)
and not MatchPattern('##:##:##.###', str)
and not MatchPattern('##:##:##', str)
and not MatchPattern('##:##', str) then // <-- Accepts HH:MM
raise EBold.CreateFmt(sInvalidTimeFormat, [str]);
But then unconditionally tried to parse seconds:
s := StrToInt(copy(str, 7, 2)); // <-- Crashes on '14:30' with '' is not a valid integer
Steps to Reproduce
var dt: TDateTime;
begin
dt := ParseISOTime('14:30'); // Raises EConvertError: '' is not a valid integer value
end;
Fix
Check string length before parsing seconds:
if Length(str) >= 8 then
begin
s := StrToInt(copy(str, 7, 2));
if s > 59 then
raise EBold.CreateFmt(sInvalidTimeFormatLargeSecond, [str]);
end
else
s := 0;
UnitTest added to testproject
Summary
ParseISOTime accepted the HH:MM format pattern but crashed when parsing because it always tried to read seconds from position 7.
Bug Description
The function pattern matching allowed ##:## format:
Steps to Reproduce
Fix
Check string length before parsing seconds:
UnitTest added to testproject