Several sheepdog functions mix up -1 and -errno in return values
Two common conventions for functions returning int that may fail:
1. Return non-negative value on success, -1 value on failure.
2. Return non-negative value on success, a negative errno error code on failure.
Both work. But mixing them in the same function is not a good idea.
Suspicious functions in block/sheepdog.c include:
* read_write_object()
May return 0, -EIO or the value of do_req(). do_req() returns srco.ret. do_co_req() may set it to the value of send_co_req(). I don't think that one returns -errno.
* do_sd_create()
May return 0, -EIO or the value of connect_to_sdog(). I don't think the latter returns -errno.
I suspect there are more. Please audit the file for this kind of mistake. Good opportunity to document for each function what it's supposed to return.
PATCH 1/1 sheepdog: fix confused return values
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause trouble error handling in the call chain.
This patch let them return -errno and add some comments.
--- block/sheepdog.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
block/sheepdog.c +11 -0
@@ -527,6 +527,7 @@ static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,return acb; } +/* Return -EIO in case of error, file descriptor on success */ static int connect_to_sdog (BDRVSheepdogState *s,Error **errp) {int fd;
@@ -546,11 +547,14 @@ static int connect_to_sdog(BDRVSheepdogState *s, Error **errp)if (fd >=0 ) {qemu_set_nonblock (fd); + }else { + fd = -EIO ; }return fd; } +/* Return 0 on success and -errno in case of error */ static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,unsigned int *wlen) {
@@ -559,11 +563,13 @@ static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, ret =qemu_co_send (sockfd, hdr,sizeof (*hdr));if (ret !=sizeof (*hdr)) {error_report ("failed to send a req, %s" ,strerror (errno)); + ret = -errno;return ret; } ret =qemu_co_send (sockfd, data, *wlen);if (ret != *wlen) { + ret = -errno;error_report ("failed to send a req, %s" ,strerror (errno)); }
@@ -638,6 +644,11 @@ out: srco->finished = true; } +/* +* Send the request to the sheep in a synchronous manner. +* +* Return 0 on success, -errno in case of error. +*/ static int do_req (int sockfd,AioContext *aio_context,SheepdogReq *hdr,void *data,unsigned int *wlen,unsigned int *rlen) {
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH] sheepdog: fix confused return values
Quoted 9 lines · Liu Yuan
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause trouble error handling in the call chain.
This patch let them return -errno and add some comments.
Reported-by Markus Armbruster <[email protected]>Signed-off-by Liu Yuan <[email protected]>
Cc Kevin and Stefan
Re: [Qemu-devel] [PATCH] sheepdog: fix confused return values
In reply to Liu YuanAm 13.02.2015 um 04:45 hat Liu Yuan geschrieben:
Quoted 41 lines
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause trouble error handling in the call chain.
This patch let them return -errno and add some comments.
Reported-by Markus Armbruster <[email protected]>Signed-off-by Liu Yuan <[email protected]>--- block/sheepdog.c | 11 +++++++++++ 1 file changed, 11 insertions(+)block/sheepdog.c +4
-0@@ -527,6 +527,7 @@ static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,return acb; } +/* Return -EIO in case of error, file descriptor on success */ static int connect_to_sdog (BDRVSheepdogState *s,Error **errp) {int fd;@@ -546,11 +547,14 @@ static int connect_to_sdog(BDRVSheepdogState *s, Error **errp)if (fd >=0 ) {qemu_set_nonblock (fd); + }else { + fd = -EIO ; }return fd; } +/* Return 0 on success and -errno in case of error */ static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,unsigned int *wlen) {
block/sheepdog.c static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, · line 566
Quoted 4 lines
block/sheepdog.c +1
-0@@ -559,11 +563,13 @@ static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, ret =qemu_co_send (sockfd, hdr,sizeof (*hdr));if (ret !=sizeof (*hdr)) {error_report ("failed to send a req, %s" ,strerror (errno)); + ret = -errno;
qemu_co_sendv_recvv() uses socket_error() internally to access the return code. This is defined as errno on POSIX, but as WSAGetLastError() on Windows.
You should probably either use socket_error() here, or change qemu_co_sendv_recvv() to return a negative error code instead of -1.
block/sheepdog.c static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, · line 574
Quoted 8 lines
block/sheepdog.c +1
-0return ret; } ret =qemu_co_send (sockfd, data, *wlen);if (ret != *wlen) { + ret = -errno;error_report ("failed to send a req, %s" ,strerror (errno)); }
The same here.
Kevin
Re: [Qemu-devel] [PATCH] sheepdog: fix confused return values
Quoted 57 lines · Kevin Wolf
Am 13.02.2015 um 04:45 hat Liu Yuan geschrieben:
Quoted 41 lines
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause trouble error handling in the call chain.
This patch let them return -errno and add some comments.
Reported-by Markus Armbruster <[email protected]>Signed-off-by Liu Yuan <[email protected]>--- block/sheepdog.c | 11 +++++++++++ 1 file changed, 11 insertions(+)block/sheepdog.c +4
-0@@ -527,6 +527,7 @@ static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,return acb; } +/* Return -EIO in case of error, file descriptor on success */ static int connect_to_sdog (BDRVSheepdogState *s,Error **errp) {int fd;@@ -546,11 +547,14 @@ static int connect_to_sdog(BDRVSheepdogState *s, Error **errp)if (fd >=0 ) {qemu_set_nonblock (fd); + }else { + fd = -EIO ; }return fd; } +/* Return 0 on success and -errno in case of error */ static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,unsigned int *wlen) {block/sheepdog.c static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, · line 566
Quoted 4 lines
block/sheepdog.c +1
-0@@ -559,11 +563,13 @@ static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, ret =qemu_co_send (sockfd, hdr,sizeof (*hdr));if (ret !=sizeof (*hdr)) {error_report ("failed to send a req, %s" ,strerror (errno)); + ret = -errno;
qemu_co_sendv_recvv()usessocket_error()internally to access the return code. This is defined as errno on POSIX, but asWSAGetLastError()on Windows.You should probably either use
socket_error()here, or changeqemu_co_sendv_recvv()to return a negative error code instead of -1.Quoted 3 lines
block/sheepdog.c +0
-0return ret; }
Quoted 8 lines
block/sheepdog.c static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data, · line 574
Quoted 5 lines
block/sheepdog.c +1
-0ret =qemu_co_send (sockfd, data, *wlen);if (ret != *wlen) { + ret = -errno;error_report ("failed to send a req, %s" ,strerror (errno)); }The same here.
Hi, Kevin, thanks for your tips. I'll post v2 against your comments.
Yuan
Re: [Qemu-devel] [PATCH] sheepdog: fix confused return values
In reply to Liu YuanQuoted 9 lines · Liu Yuan
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause trouble error handling in the call chain.
This patch let them return -errno and add some comments.
Reported-by Markus Armbruster <[email protected]>Signed-off-by Liu Yuan <[email protected]>
Did you review all functions returning negative errno, or just the two that caught my eye?
Re: [Qemu-devel] [PATCH] sheepdog: fix confused return values
Quoted 7 lines · Markus Armbruster
Quoted 5 lines · Liu Yuan
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause trouble error handling in the call chain.
Quoted 7 lines
Quoted 4 lines
This patch let them return -errno and add some comments.
Reported-by Markus Armbruster <[email protected]>Signed-off-by Liu Yuan <[email protected]>Did you review all functions returning negative errno, or just the two that caught my eye?
Umm, mostly the two you mentioned.
Yuan
Re: [Qemu-devel] [PATCH] sheepdog: fix confused return values
Quoted 10 lines · Liu Yuan
Quoted 9 lines · Markus Armbruster
Quoted 7 lines · Liu Yuan
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause trouble error handling in the call chain.
This patch let them return -errno and add some comments.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Reported-by Markus Armbruster <[email protected]>Signed-off-by Liu Yuan <[email protected]>Did you review all functions returning negative errno, or just the two that caught my eye?
Umm, mostly the two you mentioned.
I encourage you to review the whole file for this error pattern! In my experience, bugs occur in clusters. When you find one, there's a high chance similar ones exist, and looking for them is good practice.
Re: [Qemu-devel] [PATCH] sheepdog: fix confused return values
Quoted 15 lines · Markus Armbruster
Quoted 13 lines · Liu Yuan
Quoted 12 lines · Markus Armbruster
Quoted 9 lines · Liu Yuan
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause trouble error handling in the call chain.
This patch let them return -errno and add some comments.
Reported-by Markus Armbruster <[email protected]>Signed-off-by Liu Yuan <[email protected]>
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Did you review all functions returning negative errno, or just the two that caught my eye?
Umm, mostly the two you mentioned.
I encourage you to review the whole file for this error pattern! In my experience, bugs occur in clusters. When you find one, there's a high chance similar ones exist, and looking for them is good practice.
Hi Markus, I've checked the whole file as possible as I can. I can't find more ret mixing bugs.
Yuan
Re: [Qemu-devel] [PATCH] sheepdog: fix confused return values
Quoted 20 lines · Liu Yuan
Quoted 19 lines · Markus Armbruster
Quoted 17 lines · Liu Yuan
Quoted 15 lines · Markus Armbruster
Quoted 10 lines · Liu Yuan
From: Liu Yuan <[email protected]>
These functions mix up -1 and -errno in return values and would might cause
trouble error handling in the call chain.This patch let them return -errno and add some comments.
Reported-by Markus Armbruster <[email protected]>Signed-off-by Liu Yuan <[email protected]>Did you review all functions returning negative errno, or just the two that caught my eye?
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Umm, mostly the two you mentioned.
I encourage you to review the whole file for this error pattern! In my experience, bugs occur in clusters. When you find one, there's a high chance similar ones exist, and looking for them is good practice.
Hi Markus, I've checked the whole file as possible as I can. I can't find more ret mixing bugs.
Thanks!