]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util/base64: remove coverity reported dead code
authorShivani Bhardwaj <shivani@oisf.net>
Thu, 25 Apr 2024 14:17:01 +0000 (19:47 +0530)
committerVictor Julien <victor@inliniac.net>
Thu, 2 May 2024 10:44:31 +0000 (12:44 +0200)
New defect(s) Reported-by: Coverity Scan
Showing 1 of 1 defect(s)

** CID 1596621:  Control flow issues  (DEADCODE)
/src/util-base64.c: 238 in DecodeBase64RFC4648()

________________________________________________________________________________________________________
*** CID 1596621:  Control flow issues  (DEADCODE)
/src/util-base64.c: 238 in DecodeBase64RFC4648()
232         DEBUG_VALIDATE_BUG_ON(bbidx == B64_BLOCK);
233
234         /* Handle any leftover bytes by adding padding to them as long as they do not
235          * violate the destination buffer size */
236         if (bbidx > 0) {
237             padding = bbidx > 1 ? B64_BLOCK - bbidx : 2;
>>>     CID 1596621:  Control flow issues  (DEADCODE)
>>>     Execution cannot reach the expression "3U" inside this statement: "numDecoded_blk = 3U - ((pad...".
238             uint32_t numDecoded_blk = ASCII_BLOCK - (padding < B64_BLOCK ? padding : ASCII_BLOCK);
239             if (dest_size < *decoded_bytes + numDecoded_blk) {
240                 SCLogDebug("Destination buffer full");
241                 return BASE64_ECODE_BUF;
242             }
243             /* Decode base-64 block into ascii block and move pointer */

Also, add a comment explaining the padding logic for leftover data.

Bug 6985

src/util-base64.c

index bcb1261e12dbcf6034468602df9b9bb8988c6791..5b58144ad468d3054a6f325c5180486c24e607dc 100644 (file)
@@ -234,8 +234,21 @@ static inline Base64Ecode DecodeBase64RFC4648(uint8_t *dest, uint32_t dest_size,
     /* Handle any leftover bytes by adding padding to them as long as they do not
      * violate the destination buffer size */
     if (bbidx > 0) {
+        /*
+         * --------------------
+         * | bbidx  | padding |
+         * --------------------
+         * |   1    |    2    |
+         * |   2    |    2    |
+         * |   3    |    1    |
+         * --------------------
+         * Note: Padding for 1 byte is set to 2 to have at least one
+         * decoded byte while calculating numDecoded_blk
+         * This does not affect the decoding as the b64 array is already
+         * populated with all padding bytes unless overwritten.
+         * */
         padding = bbidx > 1 ? B64_BLOCK - bbidx : 2;
-        uint32_t numDecoded_blk = ASCII_BLOCK - (padding < B64_BLOCK ? padding : ASCII_BLOCK);
+        uint32_t numDecoded_blk = ASCII_BLOCK - padding;
         if (dest_size < *decoded_bytes + numDecoded_blk) {
             SCLogDebug("Destination buffer full");
             return BASE64_ECODE_BUF;