From a00b74fc7003a3bb721fef3b1fc01bc40debc41d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Tue, 30 Sep 2025 11:10:52 -0300 Subject: [PATCH] Bug 40910: Reduce log pollution for undefined/empty barcodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch prevents warnings and DBIx::Class errors when C4::SIP::ILS::Item->new() receives undefined, empty, or whitespace-only item_id values that could occur from faulty plugins or malformed SIP requests. Changes: - Add early validation for undefined/empty item_id before barcodedecode() - Add validation for barcodedecode() result before Koha::Items->find() - Add debug logging when barcodedecode empties a barcode (indicates misconfiguration) - Prevent "Odd number of elements in anonymous hash" warnings - Prevent DBIx::Class constraint validation errors - No functional changes - behavior preserved for valid barcodes Test plan: 1. Apply the regression tests 2. Run: $ ktd --shell k$ prove t/db_dependent/SIP/Item.t => FAIL: Tests fail! 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! 5. Tests cover: - Valid barcode functionality (preserved) - Undefined item_id (no warnings) - Empty string item_id (no warnings) - Whitespace-only item_id (no warnings) - barcodedecode edge cases (tabs, newlines, mixed) - Nonexistent barcodes (expected warnings preserved) 6. Verify no warnings in SIP logs for malformed requests 7. Sign off :-D Signed-off-by: Tomás Cohen Arazi Signed-off-by: Kyle M Hall --- C4/SIP/ILS/Item.pm | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/C4/SIP/ILS/Item.pm b/C4/SIP/ILS/Item.pm index 4aadb99f22d..b97a9f52bb9 100644 --- a/C4/SIP/ILS/Item.pm +++ b/C4/SIP/ILS/Item.pm @@ -81,7 +81,26 @@ Missing POD for new. sub new { my ( $class, $item_id ) = @_; my $type = ref($class) || $class; - my $item = Koha::Items->find( { barcode => barcodedecode($item_id) } ); + + # Return early if item_id is undefined, empty, or whitespace-only + if ( !defined $item_id || $item_id eq '' || $item_id =~ /^\s*$/ ) { + siplog( "LOG_DEBUG", "new ILS::Item: empty or undefined item_id" ); + return; + } + + my $decoded_barcode = barcodedecode($item_id); + + # Return early if barcode decoding results in empty value + if ( !defined $decoded_barcode || $decoded_barcode eq '' ) { + siplog( + "LOG_DEBUG", + "new ILS::Item('%s'): barcode decoding resulted in empty value - possible misconfiguration or faulty plugin", + $item_id + ); + return; + } + + my $item = Koha::Items->find( { barcode => $decoded_barcode } ); unless ($item) { siplog( "LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id ); warn "new ILS::Item($item_id) : No item '$item_id'."; -- 2.39.5 (Apple Git-154)