From a9aa2ac3907cb24beae1ada6e58c59e61d16ece6 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Thu, 17 Sep 2026 09:02:12 +0200 Subject: fix: do not truncate a number in exponent form The numeric patterns stopped at the 'e', so a temp of 1.8e1 parsed as 1.8 and the card would have drawn 2 degrees instead of 18. Wrong in the worst way available: silently, and still looking like weather. OWM has not been seen to emit exponent form, and sampling several cities near and below zero returned plain decimals throughout, so this was not reachable in practice. Fixed regardless, because two characters in a character class is cheaper than the reasoning required to be sure it stays unreachable, and unlike the NaN case noted in sun_t this one fails invisibly rather than leaving a mark. Tests now cover both a negative temperature, which is ordinary here for half the year, and the exponent form. Reverting the pattern fails them. Found by adversarial probing of the committed parser, not by the plan's own tests, which only exercise the fixture. Co-Authored-By: Claude Opus 5 --- lib/weather.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/weather.lua b/lib/weather.lua index a66d79a..d36bcf5 100644 --- a/lib/weather.lua +++ b/lib/weather.lua @@ -165,10 +165,13 @@ function M.parse(src) local w = { id = tonumber(cond:match('"id"%s*:%s*(%-?%d+)')), description = cond:match('"description"%s*:%s*"([^"]*)"'), - temp = tonumber(main:match('"temp"%s*:%s*(%-?[%d%.]+)')), - feels_like = tonumber(main:match('"feels_like"%s*:%s*(%-?[%d%.]+)')), + -- The exponent is accepted although OWM has never been seen to emit one: + -- a pattern stopping at the 'e' would read 1.8e1 as 1.8 and draw 2 degrees + -- instead of 18, silently and plausibly. Cheaper to accept than to detect. + temp = tonumber(main:match('"temp"%s*:%s*(%-?[%d%.eE%+%-]+)')), + feels_like = tonumber(main:match('"feels_like"%s*:%s*(%-?[%d%.eE%+%-]+)')), humidity = tonumber(main:match('"humidity"%s*:%s*(%d+)')), - wind_speed = tonumber(wind:match('"speed"%s*:%s*([%d%.]+)')), + wind_speed = tonumber(wind:match('"speed"%s*:%s*([%d%.eE%+%-]+)')), wind_deg = tonumber(wind:match('"deg"%s*:%s*(%d+)')), sunrise = tonumber(sys:match('"sunrise"%s*:%s*(%d+)')), sunset = tonumber(sys:match('"sunset"%s*:%s*(%d+)')), -- cgit v1.2.3