Skip to content

Commit 795d946

Browse files
Vladimir Sementsov-OgievskiyMarkus Armbruster
authored andcommitted
nbd: Use ERRP_GUARD()
If we want to check error after errp-function call, we need to introduce local_err and then propagate it to errp. Instead, use the ERRP_GUARD() macro, benefits are: 1. No need of explicit error_propagate call 2. No need of explicit local_err variable: use errp directly 3. ERRP_GUARD() leaves errp as is if it's not NULL or &error_fatal, this means that we don't break error_abort (we'll abort on error_set, not on error_propagate) If we want to add some info to errp (by error_prepend() or error_append_hint()), we must use the ERRP_GUARD() macro. Otherwise, this info will not be added when errp == &error_fatal (the program will exit prior to the error_append_hint() or error_prepend() call). Fix several such cases, e.g. in nbd_read(). This commit is generated by command sed -n '/^Network Block Device (NBD)$/,/^$/{s/^F: //p}' \ MAINTAINERS | \ xargs git ls-files | grep '\.[hc]$' | \ xargs spatch \ --sp-file scripts/coccinelle/errp-guard.cocci \ --macro-file scripts/cocci-macro-file.h \ --in-place --no-show-diff --max-width 80 Reported-by: Kevin Wolf <[email protected]> Reported-by: Greg Kurz <[email protected]> Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> Reviewed-by: Markus Armbruster <[email protected]> [Commit message tweaked] Signed-off-by: Markus Armbruster <[email protected]> Message-Id: <[email protected]> Reviewed-by: Eric Blake <[email protected]> [ERRP_AUTO_PROPAGATE() renamed to ERRP_GUARD(), and auto-propagated-errp.cocci to errp-guard.cocci. Commit message tweaked again.]
1 parent 92c4512 commit 795d946

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

block/nbd.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,16 +1408,15 @@ static void nbd_client_close(BlockDriverState *bs)
14081408
static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
14091409
Error **errp)
14101410
{
1411+
ERRP_GUARD();
14111412
QIOChannelSocket *sioc;
1412-
Error *local_err = NULL;
14131413

14141414
sioc = qio_channel_socket_new();
14151415
qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
14161416

1417-
qio_channel_socket_connect_sync(sioc, saddr, &local_err);
1418-
if (local_err) {
1417+
qio_channel_socket_connect_sync(sioc, saddr, errp);
1418+
if (*errp) {
14191419
object_unref(OBJECT(sioc));
1420-
error_propagate(errp, local_err);
14211420
return NULL;
14221421
}
14231422

include/block/nbd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ void nbd_server_start_options(NbdServerOptions *arg, Error **errp);
361361
static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
362362
const char *desc, Error **errp)
363363
{
364+
ERRP_GUARD();
364365
int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
365366

366367
if (ret < 0) {

nbd/client.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt,
6868
uint32_t len, const char *data,
6969
Error **errp)
7070
{
71+
ERRP_GUARD();
7172
NBDOption req;
7273
QEMU_BUILD_BUG_ON(sizeof(req) != 16);
7374

@@ -153,6 +154,7 @@ static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
153154
static int nbd_handle_reply_err(QIOChannel *ioc, NBDOptionReply *reply,
154155
bool strict, Error **errp)
155156
{
157+
ERRP_GUARD();
156158
g_autofree char *msg = NULL;
157159

158160
if (!(reply->type & (1 << 31))) {
@@ -337,6 +339,7 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, char **description,
337339
static int nbd_opt_info_or_go(QIOChannel *ioc, uint32_t opt,
338340
NBDExportInfo *info, Error **errp)
339341
{
342+
ERRP_GUARD();
340343
NBDOptionReply reply;
341344
uint32_t len = strlen(info->name);
342345
uint16_t type;
@@ -882,6 +885,7 @@ static int nbd_start_negotiate(AioContext *aio_context, QIOChannel *ioc,
882885
bool structured_reply, bool *zeroes,
883886
Error **errp)
884887
{
888+
ERRP_GUARD();
885889
uint64_t magic;
886890

887891
trace_nbd_start_negotiate(tlscreds, hostname ? hostname : "<null>");
@@ -1017,6 +1021,7 @@ int nbd_receive_negotiate(AioContext *aio_context, QIOChannel *ioc,
10171021
const char *hostname, QIOChannel **outioc,
10181022
NBDExportInfo *info, Error **errp)
10191023
{
1024+
ERRP_GUARD();
10201025
int result;
10211026
bool zeroes;
10221027
bool base_allocation = info->base_allocation;

nbd/server.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ static int GCC_FMT_ATTR(4, 0)
211211
nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
212212
Error **errp, const char *fmt, va_list va)
213213
{
214+
ERRP_GUARD();
214215
g_autofree char *msg = NULL;
215216
int ret;
216217
size_t len;
@@ -382,6 +383,7 @@ static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,
382383
static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
383384
Error **errp)
384385
{
386+
ERRP_GUARD();
385387
size_t name_len, desc_len;
386388
uint32_t len;
387389
const char *name = exp->name ? exp->name : "";
@@ -445,6 +447,7 @@ static void nbd_check_meta_export(NBDClient *client)
445447
static int nbd_negotiate_handle_export_name(NBDClient *client, bool no_zeroes,
446448
Error **errp)
447449
{
450+
ERRP_GUARD();
448451
g_autofree char *name = NULL;
449452
char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
450453
size_t len;
@@ -1289,6 +1292,7 @@ static int nbd_negotiate_options(NBDClient *client, Error **errp)
12891292
*/
12901293
static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
12911294
{
1295+
ERRP_GUARD();
12921296
char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
12931297
int ret;
12941298

@@ -1663,6 +1667,7 @@ void nbd_export_close(NBDExport *exp)
16631667

16641668
void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
16651669
{
1670+
ERRP_GUARD();
16661671
if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
16671672
nbd_export_close(exp);
16681673
return;

0 commit comments

Comments
 (0)