fix: add null guard in HexSKColorToByteArray to resolve S2259#87
Conversation
Co-Authored-By: Afonso Dutra Nogueira Filho <afonsoft@gmail.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
522b1c8
into
feature/devin-20260712-sonar-quality
| if (string.IsNullOrEmpty(colorString)) | ||
| throw new ArgumentException("Color string cannot be null or empty.", nameof(colorString)); |
There was a problem hiding this comment.
📝 Info: Behavioral change from silent degradation to exception for empty color strings
The old code silently handled empty color strings by producing a zero-length byte array (which would result in corrupted BMP/PDF output). The new code throws ArgumentException instead. While this is a better behavior, it is technically a breaking change for any consumer that previously relied on passing empty strings without getting an exception. I verified all current internal callers (BitmapByteQRCode.cs:52, PdfByteQRCode.cs:72, and the helper classes) and none pass null or empty — they use either hardcoded hex strings like "#000000" or forward user-provided parameters. However, since these are public API methods (GetGraphic at BitmapByteQRCode.cs:50 and PdfByteQRCode.cs:63), external consumers could be affected.
Was this helpful? React with 👍 or 👎 to provide feedback.
| if (string.IsNullOrEmpty(colorString)) | ||
| throw new ArgumentException("Color string cannot be null or empty.", nameof(colorString)); |
There was a problem hiding this comment.
📝 Info: Duplicated HexSKColorToByteArray implementation across two files
The HexSKColorToByteArray method is identically implemented in both BitmapByteQRCode.cs:113-122 and PdfByteQRCode.cs:216-225. This PR applied the same fix to both copies, but the duplication itself is a maintenance risk — future fixes or enhancements would need to be applied in both places. Consider extracting this into a shared utility (e.g., in the Extensions/ folder) to follow the DRY principle.
Was this helpful? React with 👍 or 👎 to provide feedback.



All Submissions:
New Feature Submissions:
Changes to Core Features:
fix: add null guard in HexSKColorToByteArray to resolve S2259
The
HexSKColorToByteArraymethods inPdfByteQRCodeandBitmapByteQRCodereplaced the previous!string.IsNullOrEmpty(...)checks with an explicitstring.IsNullOrEmpty(...)guard that throwsArgumentException. This ensurescolorStringis non-null and non-empty beforeSubstringis called, resolving the SonarCloudS2259issue reported on the renderer phase.Build and
dotnet test QRCoder.Core.Tests/ -f net10.0 --configuration Releasepass locally (502 tests).Link to Devin session: https://app.devin.ai/sessions/20c92a1bad724f63b8be89a9e195e664
Requested by: @afonsoft