block: Fix integer promotion error in bdrv_getlength()
Back in 2015, we attempted to fix error reporting for images that claimed to have more than INT64_MAX/512 sectors, but due to the type promotions caused by BDRV_SECTOR_SIZE being unsigned, this inadvertently forces all negative ret values to be slammed into -EFBIG rather than the original error. While we're at it, we can avoid the confusing ?: by spelling the logic more directly. Fixes: 4a9c9ea0d3 Reported-by: Guoyi Tu <tu.guoyi@h3c.com> Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <20201105155122.60943-1-eblake@redhat.com> Reviewed-by: Alberto Garcia <berto@igalia.com> Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
		
							parent
							
								
									7c5c538306
								
							
						
					
					
						commit
						122860bae7
					
				
							
								
								
									
										9
									
								
								block.c
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								block.c
									
									
									
									
									
								
							| @ -5091,8 +5091,13 @@ int64_t bdrv_getlength(BlockDriverState *bs) | |||||||
| { | { | ||||||
|     int64_t ret = bdrv_nb_sectors(bs); |     int64_t ret = bdrv_nb_sectors(bs); | ||||||
| 
 | 
 | ||||||
|     ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret; |     if (ret < 0) { | ||||||
|     return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE; |         return ret; | ||||||
|  |     } | ||||||
|  |     if (ret > INT64_MAX / BDRV_SECTOR_SIZE) { | ||||||
|  |         return -EFBIG; | ||||||
|  |     } | ||||||
|  |     return ret * BDRV_SECTOR_SIZE; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* return 0 as number of sectors if no device present or error */ | /* return 0 as number of sectors if no device present or error */ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Eric Blake
						Eric Blake