]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[bitmap] Allow bitmap_set() to report an error 1766/head
authorMichael Brown <mcb30@ipxe.org>
Thu, 30 Jul 2026 12:25:58 +0000 (13:25 +0100)
committerMichael Brown <mcb30@ipxe.org>
Thu, 30 Jul 2026 12:49:31 +0000 (13:49 +0100)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/bitmap.c
src/include/ipxe/bitmap.h
src/net/udp/slam.c
src/net/udp/tftp.c

index e3570c629cdf227c2bbd6621b6f1431b63f300f0..d44e59c628900f6438f05fa75f8b75d27c619ed5 100644 (file)
@@ -80,8 +80,10 @@ int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
        unsigned int index = BITMAP_INDEX ( bit );
         bitmap_block_t mask = BITMAP_MASK ( bit );
 
+       /* Treat out-of-range bits as implicitly being zero */
        if ( bit >= bitmap->length )
                return 0;
+
        return ( ( bitmap->blocks[index] & mask ) != 0 );
 }
 
@@ -90,13 +92,21 @@ int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) {
  *
  * @v bitmap           Bitmap
  * @v bit              Bit index
+ * @ret rc             Return status code
  */
-void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
+int bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
        unsigned int index = BITMAP_INDEX ( bit );
         bitmap_block_t mask = BITMAP_MASK ( bit );
 
        DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit );
 
+       /* Fail if we cannot set this bit */
+       if ( bit >= bitmap->length ) {
+               DBGC ( bitmap, "Bitmap %p bit %d is outside range [0,%d)\n",
+                      bitmap, bit, bitmap->length );
+               return -ERANGE;
+       }
+
        /* Update bitmap */
        bitmap->blocks[index] |= mask;
 
@@ -104,4 +114,6 @@ void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) {
        while ( bitmap_test ( bitmap, bitmap->first_gap ) ) {
                bitmap->first_gap++;
        }
+
+       return 0;
 }
index 7533d1bf9fff6f6dfa3d9a692aa85fcc81538408..b50ceb1533596b5cd91e0c86db24affdea1fc1d2 100644 (file)
@@ -48,7 +48,7 @@ struct bitmap {
 
 extern int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length );
 extern int bitmap_test ( struct bitmap *bitmap, unsigned int bit );
-extern void bitmap_set ( struct bitmap *bitmap, unsigned int bit );
+extern int bitmap_set ( struct bitmap *bitmap, unsigned int bit );
 
 /**
  * Free bitmap resources
index 47f60080b752f01cd6a93292c70b70d6d24ad5e4..dcda33750baedf11b8279fadfc4210fbdd7abf6f 100644 (file)
@@ -553,7 +553,8 @@ static int slam_mc_socket_deliver ( struct slam_request *slam,
                goto err;
 
        /* Mark block as received */
-       bitmap_set ( &slam->bitmap, packet );
+       if ( ( rc = bitmap_set ( &slam->bitmap, packet ) ) != 0 )
+               goto err;
 
        /* If we have received all blocks, terminate */
        if ( bitmap_full ( &slam->bitmap ) )
index a1d971c19ba78f63dcde69db0f372bdba56771d3..d25c0653b8969240ef4a0a5ab845414a8c1ca9c7 100644 (file)
@@ -852,7 +852,8 @@ static int tftp_rx_data ( struct tftp_request *tftp,
                goto done;
 
        /* Mark block as received */
-       bitmap_set ( &tftp->bitmap, block );
+       if ( ( rc = bitmap_set ( &tftp->bitmap, block ) ) != 0 )
+               goto done;
 
        /* Acknowledge block */
        tftp_send_packet ( tftp );