@@ -802,10 +802,17 @@ func (img *Qcow2) readAtAlignedStandardExtendedL2(p []byte, off int64, desc stan
802802	var  n  int 
803803	subclusterSize  :=  img .clusterSize  /  32 
804804	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+ 
807814		pIdxBegin  :=  n 
808- 		pIdxEnd  :=  n  +  subclusterSize 
815+ 		pIdxEnd  :=  n  +  int ( readSize ) 
809816		if  pIdxEnd  >  len (p ) {
810817			pIdxEnd  =  len (p )
811818		}
@@ -823,7 +830,6 @@ func (img *Qcow2) readAtAlignedStandardExtendedL2(p []byte, off int64, desc stan
823830				return  n , fmt .Errorf ("failed to read from the raw offset %d: %w" , currentRawOff , err )
824831			}
825832		} else  {
826- 			currentOff  :=  off  +  int64 (n )
827833			if  ((extL2Entry .ZeroStatusBitmap  >>  i ) &  0b1 ) ==  0b1  {
828834				currentN , err  =  img .readZero (p [pIdxBegin :pIdxEnd ], currentOff )
829835				if  err  !=  nil  {
@@ -902,15 +908,13 @@ func (img *Qcow2) ReadAt(p []byte, off int64) (n int, err error) {
902908
903909	for  remaining  >  0  {
904910		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 
913915
916+ 		pIndexBegin  :=  n 
917+ 		pIndexEnd  :=  n  +  int (readSize )
914918		if  pIndexEnd  >  len (p ) {
915919			pIndexEnd  =  len (p )
916920		}
0 commit comments