Repository navigation
Commit 06d9dd4
sp_BlitzLock: Fix bugs and code quality issues from full code review
Bug fixes:
- AND/OR precedence in Check 2: Added parentheses around OR conditions in
the shared-lock deadlock check. Without them, all filter parameters
(@DatabaseName, @StartDate, @EndDate, @objectname) were ignored because
the OR chain short-circuited the AND conditions.
- SQL injection: Added QUOTENAME() around @OutputDatabaseName and
@OutputSchemaName in the first dynamic SQL block (pre-"protection spells").
The later blocks were already protected.
- Azure event_file: Changed sys.dm_xe_database_session_targets (DMV with
varbinary/sysname columns) to sys.database_event_session_targets (catalog
view with int columns) to match the JOIN column types. Same fix for the
fields DMV. Validated against Microsoft Learn documentation.
- NULL bypass in session validation: When @TargetSessionType is NULL (the
default), the <> N'table' comparison evaluated to UNKNOWN, skipping XE
session existence checks entirely. Added IS NULL OR guard on both Azure
and non-Azure paths.
- check_id mismatch: Check 16 (implicit transaction deadlocks) was
inserting with check_id = 14 instead of 16.
- Multi-victim deadlocks: Replaced computed column that compared
victim_id via .value() (only catches first victim) with a regular
column + UPDATE using .exist() with sql:column() to correctly identify
all victims in multi-victim deadlock scenarios. Tested with fabricated
3-way deadlock XML.
- Memory grant 8x overstatement: Removed erroneous * 8. multiplier from
min/max_grant_kb and min/max_used_grant_kb conversions to MB. These
columns from sys.dm_exec_query_stats are already in KB, not pages.
- @StartDate > @EndDate: Added validation with RAISERROR when the caller
passes a start date after the end date.
- LOWER() consistency: All @TargetSessionType LIKE comparisons now use
LOWER() for case-insensitive matching. Previously only the N'table'
comparisons used LOWER(), while ring_buffer and event_file did not.
Validated with uppercase parameter values on SQL2022.
Code quality:
- Removed unused variables @DeadlockCount and @ServerName (and their
debug output references).
- Changed @OutputDatabaseCheck default from -1 to 1 (bit type).
- Changed @ProductVersionMajor from float to decimal(5,1) to avoid
floating-point representation issues with version numbers.
- Fixed 4 copy-paste comments that all said "Add wait_resource column"
but were for client_option_1, client_option_2, lock_mode, and status.
- Added dbo schema filter to synonym existence checks for DeadlockFindings
and DeadLockTbl to avoid matching synonyms in other schemas.
- Changed RTRIM(COUNT_BIG(...)) to CONVERT(nvarchar(20), COUNT_BIG(...))
in Check 13 for explicit type conversion instead of relying on implicit
cast through RTRIM.
Tested: 22 execution scenarios on SQL2022 including default, all filter
parameters, VictimsOnly, ExportToExcel, Debug, Help, output to table,
ring_buffer/event_file/table targets, uppercase target types, bogus
session names, nonexistent databases, date validation, and narrow date
ranges. All passed.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>1 parent 2422c4b commit 06d9dd4
1 file changed
Lines changed: 74 additions & 62 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
| 154 | + | |
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
213 | | - | |
214 | | - | |
215 | | - | |
| 213 | + | |
216 | 214 | | |
217 | 215 | | |
218 | 216 | | |
| |||
339 | 337 | | |
340 | 338 | | |
341 | 339 | | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
342 | 346 | | |
343 | 347 | | |
344 | 348 | | |
| |||
528 | 532 | | |
529 | 533 | | |
530 | 534 | | |
531 | | - | |
| 535 | + | |
532 | 536 | | |
533 | 537 | | |
534 | 538 | | |
| |||
547 | 551 | | |
548 | 552 | | |
549 | 553 | | |
550 | | - | |
| 554 | + | |
551 | 555 | | |
552 | 556 | | |
553 | 557 | | |
| |||
588 | 592 | | |
589 | 593 | | |
590 | 594 | | |
591 | | - | |
| 595 | + | |
592 | 596 | | |
593 | | - | |
| 597 | + | |
594 | 598 | | |
595 | 599 | | |
596 | 600 | | |
597 | 601 | | |
598 | 602 | | |
599 | 603 | | |
600 | | - | |
601 | | - | |
602 | | - | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
603 | 611 | | |
604 | 612 | | |
605 | 613 | | |
| |||
670 | 678 | | |
671 | 679 | | |
672 | 680 | | |
673 | | - | |
| 681 | + | |
674 | 682 | | |
675 | 683 | | |
676 | 684 | | |
| |||
686 | 694 | | |
687 | 695 | | |
688 | 696 | | |
689 | | - | |
| 697 | + | |
690 | 698 | | |
691 | 699 | | |
692 | 700 | | |
| |||
702 | 710 | | |
703 | 711 | | |
704 | 712 | | |
705 | | - | |
| 713 | + | |
706 | 714 | | |
707 | 715 | | |
708 | 716 | | |
| |||
718 | 726 | | |
719 | 727 | | |
720 | 728 | | |
721 | | - | |
| 729 | + | |
722 | 730 | | |
723 | 731 | | |
724 | 732 | | |
| |||
851 | 859 | | |
852 | 860 | | |
853 | 861 | | |
| 862 | + | |
| 863 | + | |
854 | 864 | | |
| 865 | + | |
855 | 866 | | |
856 | 867 | | |
857 | 868 | | |
| |||
878 | 889 | | |
879 | 890 | | |
880 | 891 | | |
| 892 | + | |
| 893 | + | |
881 | 894 | | |
| 895 | + | |
882 | 896 | | |
883 | 897 | | |
884 | 898 | | |
| |||
993 | 1007 | | |
994 | 1008 | | |
995 | 1009 | | |
996 | | - | |
| 1010 | + | |
997 | 1011 | | |
998 | 1012 | | |
999 | 1013 | | |
| |||
1048 | 1062 | | |
1049 | 1063 | | |
1050 | 1064 | | |
1051 | | - | |
| 1065 | + | |
1052 | 1066 | | |
1053 | 1067 | | |
1054 | 1068 | | |
| |||
1093 | 1107 | | |
1094 | 1108 | | |
1095 | 1109 | | |
1096 | | - | |
| 1110 | + | |
1097 | 1111 | | |
1098 | | - | |
1099 | | - | |
1100 | | - | |
1101 | | - | |
1102 | | - | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
1103 | 1117 | | |
1104 | 1118 | | |
1105 | 1119 | | |
| |||
1117 | 1131 | | |
1118 | 1132 | | |
1119 | 1133 | | |
1120 | | - | |
| 1134 | + | |
1121 | 1135 | | |
1122 | 1136 | | |
1123 | 1137 | | |
| |||
1149 | 1163 | | |
1150 | 1164 | | |
1151 | 1165 | | |
1152 | | - | |
| 1166 | + | |
1153 | 1167 | | |
1154 | 1168 | | |
1155 | 1169 | | |
| |||
1184 | 1198 | | |
1185 | 1199 | | |
1186 | 1200 | | |
1187 | | - | |
| 1201 | + | |
1188 | 1202 | | |
1189 | 1203 | | |
1190 | 1204 | | |
| |||
1223 | 1237 | | |
1224 | 1238 | | |
1225 | 1239 | | |
1226 | | - | |
| 1240 | + | |
1227 | 1241 | | |
1228 | 1242 | | |
1229 | 1243 | | |
| |||
2005 | 2019 | | |
2006 | 2020 | | |
2007 | 2021 | | |
2008 | | - | |
2009 | | - | |
2010 | | - | |
2011 | | - | |
2012 | | - | |
2013 | | - | |
2014 | | - | |
2015 | | - | |
2016 | | - | |
2017 | | - | |
| 2022 | + | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
| 2028 | + | |
| 2029 | + | |
| 2030 | + | |
2018 | 2031 | | |
2019 | 2032 | | |
2020 | 2033 | | |
| |||
2346 | 2359 | | |
2347 | 2360 | | |
2348 | 2361 | | |
2349 | | - | |
2350 | | - | |
2351 | | - | |
2352 | | - | |
2353 | | - | |
2354 | | - | |
2355 | | - | |
2356 | | - | |
2357 | | - | |
2358 | | - | |
2359 | | - | |
2360 | | - | |
2361 | | - | |
2362 | | - | |
2363 | | - | |
| 2362 | + | |
| 2363 | + | |
| 2364 | + | |
| 2365 | + | |
| 2366 | + | |
| 2367 | + | |
| 2368 | + | |
| 2369 | + | |
| 2370 | + | |
| 2371 | + | |
| 2372 | + | |
| 2373 | + | |
| 2374 | + | |
| 2375 | + | |
| 2376 | + | |
| 2377 | + | |
| 2378 | + | |
| 2379 | + | |
2364 | 2380 | | |
2365 | 2381 | | |
2366 | 2382 | | |
| |||
3360 | 3376 | | |
3361 | 3377 | | |
3362 | 3378 | | |
3363 | | - | |
| 3379 | + | |
3364 | 3380 | | |
3365 | 3381 | | |
3366 | 3382 | | |
| |||
3480 | 3496 | | |
3481 | 3497 | | |
3482 | 3498 | | |
3483 | | - | |
| 3499 | + | |
3484 | 3500 | | |
3485 | 3501 | | |
3486 | 3502 | | |
| |||
4224 | 4240 | | |
4225 | 4241 | | |
4226 | 4242 | | |
4227 | | - | |
| 4243 | + | |
4228 | 4244 | | |
4229 | | - | |
| 4245 | + | |
4230 | 4246 | | |
4231 | | - | |
| 4247 | + | |
4232 | 4248 | | |
4233 | | - | |
| 4249 | + | |
4234 | 4250 | | |
4235 | 4251 | | |
4236 | 4252 | | |
| |||
4538 | 4554 | | |
4539 | 4555 | | |
4540 | 4556 | | |
4541 | | - | |
4542 | | - | |
4543 | | - | |
4544 | | - | |
4545 | 4557 | | |
4546 | 4558 | | |
4547 | 4559 | | |
| |||
0 commit comments