Skip to content

Commit f594e12

Browse files
Fix sp_LogHunter custom_message_only validation and canary date floor
Two related bugs in the setup path: 1. @custom_message_only = 1 with NULL/empty @custom_message produced an empty #search. The canary and canned-search inserts are gated on @custom_message_only = 0, so they skipped; the custom insert is gated on @custom_message LIKE N'_%' which is UNKNOWN (treated as FALSE) when @custom_message is NULL, so it also skipped. The outer cursor ran over nothing and the sproc returned "no results" on what should have been an invalid input. Added an early validation that RAISERRORs a clear message instead. 2. Canary searches built `days_back` as a "90 days ago at minimum" floor via CASE WHEN @days_back > -90 THEN -90 ELSE @days_back END. When the caller passes @start_date/@end_date instead of @days_back, the setup code NULLs @days_back — and CASE over NULL collapses to NULL, the CONVERT→DATEADD produces NULL, and the final concatenated literal becomes the string "NULL" (or NULLs all the way out), which xp_readerrorlog rejects. Switched to a CASE WHEN @days_back IS NOT NULL fallback that substitutes @start_date when date-range mode is active. Verified on SQL Server 2022: @custom_message_only = 1 (no message) → expected error raised @start_date / @end_date set, @days_back NULL → canary rows build with a real date floor and xp_readerrorlog runs without error Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 85cc60e commit f594e12

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

sp_LogHunter/sp_LogHunter.sql

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ BEGIN
241241
@custom_message_only = 0;
242242
END;
243243

244+
/*
245+
@custom_message_only = 1 means "skip the canned search strings and
246+
look only for the user-supplied @custom_message". Without a message
247+
to search for, every insert branch below would skip (the custom
248+
insert is gated on @custom_message LIKE N'_%', which is NULL/false
249+
for NULL or empty input), leaving #search empty and the whole
250+
outer loop a no-op. Reject the combination up front.
251+
*/
252+
IF @custom_message_only = 1
253+
AND (@custom_message IS NULL OR LEN(@custom_message) = 0)
254+
BEGIN
255+
RAISERROR(N'@custom_message_only = 1 requires a non-empty @custom_message. Provide a search string or set @custom_message_only = 0.', 11, 1) WITH NOWAIT;
256+
RETURN;
257+
END;
258+
244259
/*Fix @end_date*/
245260
IF @start_date IS NOT NULL
246261
AND @end_date IS NULL
@@ -412,8 +427,24 @@ BEGIN
412427
CROSS JOIN
413428
(
414429
SELECT
430+
/*
431+
Canary floor is normally "at least 90 days back" so these
432+
server-identity strings are found regardless of how recent
433+
the caller is interested in. When the caller supplied
434+
@start_date/@end_date, @days_back is NULL at this point —
435+
the previous CASE collapsed to NULL, produced a NULL
436+
days_back literal, and xp_readerrorlog received NULL as a
437+
date argument and errored. Fall back to @start_date in
438+
date-range mode so the canary has a concrete floor.
439+
*/
415440
days_back =
416-
N'"' + CONVERT(nvarchar(10), DATEADD(DAY, CASE WHEN @days_back > -90 THEN -90 ELSE @days_back END, SYSDATETIME()), 112) + N'"',
441+
N'"' +
442+
CASE
443+
WHEN @days_back IS NOT NULL
444+
THEN CONVERT(nvarchar(10), DATEADD(DAY, CASE WHEN @days_back > -90 THEN -90 ELSE @days_back END, SYSDATETIME()), 112)
445+
ELSE CONVERT(nvarchar(10), @start_date, 112)
446+
END +
447+
N'"',
417448
start_date =
418449
N'"' + CONVERT(nvarchar(30), @start_date) + N'"',
419450
end_date =

0 commit comments

Comments
 (0)