Skip to content

Commit

Permalink
refactor some more error path code
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Nov 10, 2024
1 parent 55c5575 commit 2154b5f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 54 deletions.
60 changes: 23 additions & 37 deletions src/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {

const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
nano_aio *saio;
nng_msg *msg = NULL;
SEXP aio, env, fun;
nano_buf buf;
int sock, xc;
Expand All @@ -430,18 +431,14 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
if ((sock = ptrtag == nano_SocketSymbol) || ptrtag == nano_ContextSymbol) {

nano_encodes(mode, &buf, data, NANO_PROT(con));
nng_msg *msg;

saio = R_Calloc(1, nano_aio);
saio->type = SENDAIO;

if ((xc = nng_msg_alloc(&msg, 0)))
goto exitlevel1;

if ((xc = nng_msg_append(msg, buf.buf, buf.cur)) ||
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
nng_msg_free(msg);
goto exitlevel1;
}
if ((xc = nng_msg_alloc(&msg, 0)) ||
(xc = nng_msg_append(msg, buf.buf, buf.cur)) ||
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio)))
goto fail;

nng_aio_set_msg(saio->aio, msg);
nng_aio_set_timeout(saio->aio, dur);
Expand All @@ -467,11 +464,9 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
iov.iov_len = buf.cur - nst->textframes;
iov.iov_buf = saio->data;

if ((xc = nng_aio_alloc(&saio->aio, isaio_complete, saio)))
goto exitlevel2;

if ((xc = nng_aio_set_iov(saio->aio, 1u, &iov)))
goto exitlevel3;
if ((xc = nng_aio_alloc(&saio->aio, isaio_complete, saio)) ||
(xc = nng_aio_set_iov(saio->aio, 1u, &iov)))
goto fail;

nng_aio_set_timeout(saio->aio, dur);
nng_stream_send(sp, saio->aio);
Expand All @@ -486,18 +481,14 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
nng_socket sock = nng_pipe_socket(*p);

nano_encodes(mode, &buf, data, NANO_PROT(con));
nng_msg *msg;

saio = R_Calloc(1, nano_aio);
saio->type = SENDAIO;

if ((xc = nng_msg_alloc(&msg, 0)))
goto exitlevel1;

if ((xc = nng_msg_append(msg, buf.buf, buf.cur)) ||
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
nng_msg_free(msg);
goto exitlevel1;
}
if ((xc = nng_msg_alloc(&msg, 0)) ||
(xc = nng_msg_append(msg, buf.buf, buf.cur)) ||
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio)))
goto fail;

nng_msg_set_pipe(msg, *p);
nng_aio_set_msg(saio->aio, msg);
Expand All @@ -522,11 +513,10 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
UNPROTECT(3);
return env;

exitlevel3:
nng_aio_free(saio->aio);
exitlevel2:
fail:
if (saio->aio) nng_aio_free(saio->aio);
R_Free(saio->data);
exitlevel1:
if (msg) nng_msg_free(msg);
R_Free(saio);
NANO_FREE(buf);
return mk_error_data(-xc);
Expand All @@ -553,7 +543,7 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP cvar, SEXP bytes, SEX
raio->cb = NULL;

if ((xc = nng_aio_alloc(&raio->aio, signal ? raio_complete_signal : raio_complete, raio)))
goto exitlevel1;
goto fail;

nng_aio_set_timeout(raio->aio, dur);
sock ? nng_recv_aio(*(nng_socket *) NANO_PTR(con), raio->aio) :
Expand All @@ -578,11 +568,9 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP cvar, SEXP bytes, SEX
iov.iov_len = xlen;
iov.iov_buf = raio->data;

if ((xc = nng_aio_alloc(&raio->aio, signal ? iraio_complete_signal : iraio_complete, raio)))
goto exitlevel2;

if ((xc = nng_aio_set_iov(raio->aio, 1u, &iov)))
goto exitlevel3;
if ((xc = nng_aio_alloc(&raio->aio, signal ? iraio_complete_signal : iraio_complete, raio)) ||
(xc = nng_aio_set_iov(raio->aio, 1u, &iov)))
goto fail;

nng_aio_set_timeout(raio->aio, dur);
nng_stream_recv(*sp, raio->aio);
Expand All @@ -604,11 +592,9 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP cvar, SEXP bytes, SEX
UNPROTECT(3);
return env;

exitlevel3:
nng_aio_free(raio->aio);
exitlevel2:
fail:
if (raio->aio) nng_aio_free(raio->aio);
R_Free(raio->data);
exitlevel1:
R_Free(raio);
return mk_error_data(xc);

Expand Down
12 changes: 4 additions & 8 deletions src/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,8 @@ SEXP rnng_cv_alloc(void) {
SEXP xp;
int xc;

if ((xc = nng_mtx_alloc(&cvp->mtx)))
goto fail;

if ((xc = nng_cv_alloc(&cvp->cv, cvp->mtx)))
if ((xc = nng_mtx_alloc(&cvp->mtx)) ||
(xc = nng_cv_alloc(&cvp->cv, cvp->mtx)))
goto fail;

PROTECT(xp = R_MakeExternalPtr(cvp, nano_CvSymbol, R_NilValue));
Expand Down Expand Up @@ -427,10 +425,8 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou

nng_msg *msg = NULL;

if ((xc = nng_msg_alloc(&msg, 0)))
goto fail;

if ((xc = nng_msg_append(msg, buf.buf, buf.cur)) ||
if ((xc = nng_msg_alloc(&msg, 0)) ||
(xc = nng_msg_append(msg, buf.buf, buf.cur)) ||
(xc = nng_aio_alloc(&saio->aio, sendaio_complete, saio)))
goto fail;

Expand Down
15 changes: 6 additions & 9 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,8 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
if (client != R_NilValue) {
file = CHAR(STRING_ELT(client, 0));
usefile = XLENGTH(client);
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)))
goto fail;
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)) ||
(xc = nng_tls_config_auth_mode(cfg, mod)))
goto fail;

if (usefile > 1) {
Expand All @@ -305,9 +304,8 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
file = CHAR(STRING_ELT(server, 0));
usefile = XLENGTH(server);
pss = pass != R_NilValue ? CHAR(STRING_ELT(pass, 0)) : NULL;
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_SERVER)))
goto fail;
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_SERVER)) ||
(xc = nng_tls_config_auth_mode(cfg, mod)))
goto fail;

if (usefile > 1) {
Expand All @@ -321,9 +319,8 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
}

} else {
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)))
goto fail;
if ((xc = nng_tls_config_auth_mode(cfg, NNG_TLS_AUTH_MODE_NONE)))
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)) ||
(xc = nng_tls_config_auth_mode(cfg, NNG_TLS_AUTH_MODE_NONE)))
goto fail;
}

Expand Down

0 comments on commit 2154b5f

Please sign in to comment.