@@ -1862,71 +1862,73 @@ static int check_refcounts_l1(BlockDriverState *bs,
1862
1862
int flags , BdrvCheckMode fix , bool active )
1863
1863
{
1864
1864
BDRVQcow2State * s = bs -> opaque ;
1865
- uint64_t * l1_table = NULL , l2_offset , l1_size2 ;
1865
+ size_t l1_size_bytes = l1_size * L1E_SIZE ;
1866
+ g_autofree uint64_t * l1_table = NULL ;
1867
+ uint64_t l2_offset ;
1866
1868
int i , ret ;
1867
1869
1868
- l1_size2 = l1_size * L1E_SIZE ;
1870
+ if (!l1_size ) {
1871
+ return 0 ;
1872
+ }
1869
1873
1870
1874
/* Mark L1 table as used */
1871
1875
ret = qcow2_inc_refcounts_imrt (bs , res , refcount_table , refcount_table_size ,
1872
- l1_table_offset , l1_size2 );
1876
+ l1_table_offset , l1_size_bytes );
1873
1877
if (ret < 0 ) {
1874
- goto fail ;
1878
+ return ret ;
1879
+ }
1880
+
1881
+ l1_table = g_try_malloc (l1_size_bytes );
1882
+ if (l1_table == NULL ) {
1883
+ res -> check_errors ++ ;
1884
+ return - ENOMEM ;
1875
1885
}
1876
1886
1877
1887
/* Read L1 table entries from disk */
1878
- if (l1_size2 > 0 ) {
1879
- l1_table = g_try_malloc (l1_size2 );
1880
- if (l1_table == NULL ) {
1881
- ret = - ENOMEM ;
1882
- res -> check_errors ++ ;
1883
- goto fail ;
1884
- }
1885
- ret = bdrv_pread (bs -> file , l1_table_offset , l1_table , l1_size2 );
1886
- if (ret < 0 ) {
1887
- fprintf (stderr , "ERROR: I/O error in check_refcounts_l1\n" );
1888
- res -> check_errors ++ ;
1889
- goto fail ;
1890
- }
1891
- for (i = 0 ;i < l1_size ; i ++ )
1892
- be64_to_cpus (& l1_table [i ]);
1888
+ ret = bdrv_pread (bs -> file , l1_table_offset , l1_table , l1_size_bytes );
1889
+ if (ret < 0 ) {
1890
+ fprintf (stderr , "ERROR: I/O error in check_refcounts_l1\n" );
1891
+ res -> check_errors ++ ;
1892
+ return ret ;
1893
+ }
1894
+
1895
+ for (i = 0 ; i < l1_size ; i ++ ) {
1896
+ be64_to_cpus (& l1_table [i ]);
1893
1897
}
1894
1898
1895
1899
/* Do the actual checks */
1896
- for (i = 0 ; i < l1_size ; i ++ ) {
1897
- l2_offset = l1_table [i ];
1898
- if (l2_offset ) {
1899
- /* Mark L2 table as used */
1900
- l2_offset &= L1E_OFFSET_MASK ;
1901
- ret = qcow2_inc_refcounts_imrt (bs , res ,
1902
- refcount_table , refcount_table_size ,
1903
- l2_offset , s -> cluster_size );
1904
- if (ret < 0 ) {
1905
- goto fail ;
1906
- }
1900
+ for (i = 0 ; i < l1_size ; i ++ ) {
1901
+ if (!l1_table [i ]) {
1902
+ continue ;
1903
+ }
1907
1904
1908
- /* L2 tables are cluster aligned */
1909
- if (offset_into_cluster (s , l2_offset )) {
1910
- fprintf (stderr , "ERROR l2_offset=%" PRIx64 ": Table is not "
1911
- "cluster aligned; L1 entry corrupted\n" , l2_offset );
1912
- res -> corruptions ++ ;
1913
- }
1905
+ l2_offset = l1_table [i ] & L1E_OFFSET_MASK ;
1914
1906
1915
- /* Process and check L2 entries */
1916
- ret = check_refcounts_l2 (bs , res , refcount_table ,
1917
- refcount_table_size , l2_offset , flags ,
1918
- fix , active );
1919
- if (ret < 0 ) {
1920
- goto fail ;
1921
- }
1907
+ /* Mark L2 table as used */
1908
+ ret = qcow2_inc_refcounts_imrt (bs , res ,
1909
+ refcount_table , refcount_table_size ,
1910
+ l2_offset , s -> cluster_size );
1911
+ if (ret < 0 ) {
1912
+ return ret ;
1913
+ }
1914
+
1915
+ /* L2 tables are cluster aligned */
1916
+ if (offset_into_cluster (s , l2_offset )) {
1917
+ fprintf (stderr , "ERROR l2_offset=%" PRIx64 ": Table is not "
1918
+ "cluster aligned; L1 entry corrupted\n" , l2_offset );
1919
+ res -> corruptions ++ ;
1920
+ }
1921
+
1922
+ /* Process and check L2 entries */
1923
+ ret = check_refcounts_l2 (bs , res , refcount_table ,
1924
+ refcount_table_size , l2_offset , flags ,
1925
+ fix , active );
1926
+ if (ret < 0 ) {
1927
+ return ret ;
1922
1928
}
1923
1929
}
1924
- g_free (l1_table );
1925
- return 0 ;
1926
1930
1927
- fail :
1928
- g_free (l1_table );
1929
- return ret ;
1931
+ return 0 ;
1930
1932
}
1931
1933
1932
1934
/*
0 commit comments