@@ -802,10 +802,17 @@ func (img *Qcow2) readAtAlignedStandardExtendedL2(p []byte, off int64, desc stan
802
802
var n int
803
803
subclusterSize := img .clusterSize / 32
804
804
hostClusterOffset := desc .hostClusterOffset ()
805
- subclusterNo := (int (off ) % img .clusterSize ) / subclusterSize
806
- for i := subclusterNo ; i < 32 ; i ++ {
805
+ subclusterNoBegin := (int (off ) % img .clusterSize ) / subclusterSize
806
+ for i := subclusterNoBegin ; i < 32 ; i ++ { // i is the subcluster number
807
+ currentOff := off + int64 (n )
808
+ clusterNo := currentOff / int64 (img .clusterSize )
809
+ clusterBegin := clusterNo * int64 (img .clusterSize )
810
+ subclusterBegin := clusterBegin + int64 (i )* int64 (subclusterSize )
811
+ subclusterEnd := subclusterBegin + int64 (subclusterSize )
812
+ readSize := subclusterEnd - currentOff
813
+
807
814
pIdxBegin := n
808
- pIdxEnd := n + subclusterSize
815
+ pIdxEnd := n + int ( readSize )
809
816
if pIdxEnd > len (p ) {
810
817
pIdxEnd = len (p )
811
818
}
@@ -823,7 +830,6 @@ func (img *Qcow2) readAtAlignedStandardExtendedL2(p []byte, off int64, desc stan
823
830
return n , fmt .Errorf ("failed to read from the raw offset %d: %w" , currentRawOff , err )
824
831
}
825
832
} else {
826
- currentOff := off + int64 (n )
827
833
if ((extL2Entry .ZeroStatusBitmap >> i ) & 0b1 ) == 0b1 {
828
834
currentN , err = img .readZero (p [pIdxBegin :pIdxEnd ], currentOff )
829
835
if err != nil {
@@ -902,15 +908,13 @@ func (img *Qcow2) ReadAt(p []byte, off int64) (n int, err error) {
902
908
903
909
for remaining > 0 {
904
910
currentOff := off + int64 (n )
905
- pIndexBegin := n
906
- pIndexEnd := n + int (img .clusterSize )
907
-
908
- clusterBegin := (off + int64 (pIndexBegin )) / int64 (img .clusterSize )
909
- if clusterEnd := (off + int64 (pIndexEnd )) / int64 (img .clusterSize ); clusterEnd != clusterBegin {
910
- currentSize := off + int64 (img .clusterSize ) - int64 (n )
911
- pIndexEnd = pIndexBegin + int (currentSize )
912
- }
911
+ clusterNo := currentOff / int64 (img .clusterSize )
912
+ clusterBegin := clusterNo * int64 (img .clusterSize )
913
+ clusterEnd := clusterBegin + int64 (img .clusterSize )
914
+ readSize := clusterEnd - currentOff
913
915
916
+ pIndexBegin := n
917
+ pIndexEnd := n + int (readSize )
914
918
if pIndexEnd > len (p ) {
915
919
pIndexEnd = len (p )
916
920
}
0 commit comments