Skip to content

Commit 365911b

Browse files
stefanhaRHkevmw
authored andcommitted
qemu-io: add cvtnum() error handling for zone commands
cvtnum() parses positive int64_t values and returns a negative errno on failure. Print errors and return early when cvtnum() fails. While we're at it, also reject nr_zones values greater or equal to 2^32 since they cannot be represented. Reported-by: Peter Maydell <[email protected]> Cc: Sam Li <[email protected]> Signed-off-by: Stefan Hajnoczi <[email protected]> Message-ID: <[email protected]> Reviewed-by: Sam Li <[email protected]> Reviewed-by: Kevin Wolf <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent e669e80 commit 365911b

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

qemu-io-cmds.c

+47-1
Original file line numberDiff line numberDiff line change
@@ -1739,12 +1739,26 @@ static int zone_report_f(BlockBackend *blk, int argc, char **argv)
17391739
{
17401740
int ret;
17411741
int64_t offset;
1742+
int64_t val;
17421743
unsigned int nr_zones;
17431744

17441745
++optind;
17451746
offset = cvtnum(argv[optind]);
1747+
if (offset < 0) {
1748+
print_cvtnum_err(offset, argv[optind]);
1749+
return offset;
1750+
}
17461751
++optind;
1747-
nr_zones = cvtnum(argv[optind]);
1752+
val = cvtnum(argv[optind]);
1753+
if (val < 0) {
1754+
print_cvtnum_err(val, argv[optind]);
1755+
return val;
1756+
}
1757+
if (val > UINT_MAX) {
1758+
printf("Number of zones must be less than 2^32\n");
1759+
return -ERANGE;
1760+
}
1761+
nr_zones = val;
17481762

17491763
g_autofree BlockZoneDescriptor *zones = NULL;
17501764
zones = g_new(BlockZoneDescriptor, nr_zones);
@@ -1780,8 +1794,16 @@ static int zone_open_f(BlockBackend *blk, int argc, char **argv)
17801794
int64_t offset, len;
17811795
++optind;
17821796
offset = cvtnum(argv[optind]);
1797+
if (offset < 0) {
1798+
print_cvtnum_err(offset, argv[optind]);
1799+
return offset;
1800+
}
17831801
++optind;
17841802
len = cvtnum(argv[optind]);
1803+
if (len < 0) {
1804+
print_cvtnum_err(len, argv[optind]);
1805+
return len;
1806+
}
17851807
ret = blk_zone_mgmt(blk, BLK_ZO_OPEN, offset, len);
17861808
if (ret < 0) {
17871809
printf("zone open failed: %s\n", strerror(-ret));
@@ -1805,8 +1827,16 @@ static int zone_close_f(BlockBackend *blk, int argc, char **argv)
18051827
int64_t offset, len;
18061828
++optind;
18071829
offset = cvtnum(argv[optind]);
1830+
if (offset < 0) {
1831+
print_cvtnum_err(offset, argv[optind]);
1832+
return offset;
1833+
}
18081834
++optind;
18091835
len = cvtnum(argv[optind]);
1836+
if (len < 0) {
1837+
print_cvtnum_err(len, argv[optind]);
1838+
return len;
1839+
}
18101840
ret = blk_zone_mgmt(blk, BLK_ZO_CLOSE, offset, len);
18111841
if (ret < 0) {
18121842
printf("zone close failed: %s\n", strerror(-ret));
@@ -1830,8 +1860,16 @@ static int zone_finish_f(BlockBackend *blk, int argc, char **argv)
18301860
int64_t offset, len;
18311861
++optind;
18321862
offset = cvtnum(argv[optind]);
1863+
if (offset < 0) {
1864+
print_cvtnum_err(offset, argv[optind]);
1865+
return offset;
1866+
}
18331867
++optind;
18341868
len = cvtnum(argv[optind]);
1869+
if (len < 0) {
1870+
print_cvtnum_err(len, argv[optind]);
1871+
return len;
1872+
}
18351873
ret = blk_zone_mgmt(blk, BLK_ZO_FINISH, offset, len);
18361874
if (ret < 0) {
18371875
printf("zone finish failed: %s\n", strerror(-ret));
@@ -1855,8 +1893,16 @@ static int zone_reset_f(BlockBackend *blk, int argc, char **argv)
18551893
int64_t offset, len;
18561894
++optind;
18571895
offset = cvtnum(argv[optind]);
1896+
if (offset < 0) {
1897+
print_cvtnum_err(offset, argv[optind]);
1898+
return offset;
1899+
}
18581900
++optind;
18591901
len = cvtnum(argv[optind]);
1902+
if (len < 0) {
1903+
print_cvtnum_err(len, argv[optind]);
1904+
return len;
1905+
}
18601906
ret = blk_zone_mgmt(blk, BLK_ZO_RESET, offset, len);
18611907
if (ret < 0) {
18621908
printf("zone reset failed: %s\n", strerror(-ret));

0 commit comments

Comments
 (0)