Skip to content

Commit

Permalink
refactor some error path code
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Nov 9, 2024
1 parent 963dfbf commit 55c5575
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 48 deletions.
9 changes: 4 additions & 5 deletions src/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ SEXP rnng_reap(SEXP con) {
SEXP rnng_aio_collect_pipe(SEXP aio) {

if (TYPEOF(aio) != ENVSXP)
goto exitlevel1;
goto fail;
const SEXP coreaio = nano_findVarInFrame(aio, nano_AioSymbol);
if (NANO_TAG(coreaio) != nano_AioSymbol)
goto exitlevel1;
goto fail;

nano_aio *aiop = (nano_aio *) NANO_PTR(coreaio);
switch (aiop->type) {
Expand All @@ -239,7 +239,7 @@ SEXP rnng_aio_collect_pipe(SEXP aio) {
case SENDAIO:
case IOV_SENDAIO:
case HTTP_AIO:
goto exitlevel1;
goto fail;
}

nng_pipe *p;
Expand All @@ -260,9 +260,8 @@ SEXP rnng_aio_collect_pipe(SEXP aio) {
UNPROTECT(1);
return pipe;

exitlevel1:
fail:
Rf_error("'x' is not a valid or active recvAio");
return R_NilValue;

}

Expand Down
40 changes: 18 additions & 22 deletions src/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ SEXP rnng_cv_alloc(void) {
int xc;

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

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

PROTECT(xp = R_MakeExternalPtr(cvp, nano_CvSymbol, R_NilValue));
R_RegisterCFinalizerEx(xp, cv_finalizer, TRUE);
Expand All @@ -196,10 +196,9 @@ SEXP rnng_cv_alloc(void) {
UNPROTECT(1);
return xp;

exitlevel2:
nng_mtx_free(cvp->mtx);
exitlevel1:
R_Free(cvp);
fail:
if (cvp->mtx) nng_mtx_free(cvp->mtx);
R_Free(cvp);
ERROR_OUT(xc);

}
Expand Down Expand Up @@ -420,35 +419,32 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou

SEXP aio, env, fun;
nano_buf buf;
nano_saio *saio;
nano_aio *raio;
nng_msg *msg;
int xc;

nano_encodes(sendmode, &buf, data, NANO_PROT(con));
saio = R_Calloc(1, nano_saio);
saio->cb = NULL;

int xc;
nano_saio *saio = R_Calloc(1, nano_saio);

nng_msg *msg = NULL;

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

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

nng_aio_set_msg(saio->aio, msg);
nng_ctx_send(*ctx, saio->aio);

raio = R_Calloc(1, nano_aio);
nano_aio *raio = R_Calloc(1, nano_aio);
raio->type = signal ? REQAIOS : REQAIO;
raio->mode = mod;
raio->cb = saio;
raio->next = ncv;

if ((xc = nng_aio_alloc(&raio->aio, signal ? request_complete_signal : drop ? request_complete_dropcon : request_complete, raio)))
goto exitlevel2;
goto fail;

nng_aio_set_timeout(raio->aio, dur);
nng_ctx_recv(*ctx, raio->aio);
Expand All @@ -467,10 +463,10 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
UNPROTECT(3);
return env;

exitlevel2:
fail:
R_Free(raio);
nng_aio_free(saio->aio);
exitlevel1:
if (saio->aio) nng_aio_free(saio->aio);
if (msg) nng_msg_free(msg);
R_Free(saio);
NANO_FREE(buf);
return mk_error_data(xc);
Expand Down
41 changes: 20 additions & 21 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,17 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {

if ((xc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen(pers))) ||
(xc = mbedtls_pk_setup(&key, mbedtls_pk_info_from_type((mbedtls_pk_type_t) MBEDTLS_PK_RSA))))
goto exitlevel1;
goto fail;

if (interactive) REprintf("\rGenerating key + certificate [.. ]");

if ((xc = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg, 4096, 65537)))
goto exitlevel1;
goto fail;

if (interactive) REprintf("\rGenerating key + certificate [... ]");

if ((xc = mbedtls_pk_write_key_pem(&key, key_buf, 16000)))
goto exitlevel1;
goto fail;

size_t klen = strlen((char *) key_buf);

Expand All @@ -210,14 +210,14 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
#else
(xc = mbedtls_pk_parse_key(&loaded_issuer_key, key_buf, klen + 1, NULL, 0)))
#endif
goto exitlevel1;
goto fail;

mbedtls_x509write_crt_set_subject_key(&crt, issuer_key);
mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);

if ((xc = mbedtls_x509write_crt_set_subject_name(&crt, issuer_name)) ||
(xc = mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name)))
goto exitlevel1;
goto fail;

mbedtls_x509write_crt_set_version(&crt, version);
mbedtls_x509write_crt_set_md_alg(&crt, md);
Expand All @@ -232,7 +232,7 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
(xc = mbedtls_x509write_crt_set_subject_key_identifier(&crt)) ||
(xc = mbedtls_x509write_crt_set_authority_key_identifier(&crt)) ||
(xc = mbedtls_x509write_crt_pem(&crt, output_buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg)))
goto exitlevel1;
goto fail;

SEXP vec, kcstr, cstr;
const char *names[] = {"server", "client", ""};
Expand All @@ -249,7 +249,7 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
if (interactive) REprintf("\rGenerating key + certificate [done]\n");
exit = 0;

exitlevel1:
fail:

mbedtls_x509_csr_free(&csr); // #if defined(MBEDTLS_X509_CSR_PARSE_C)
mbedtls_x509_crt_free(&issuer_crt);
Expand Down Expand Up @@ -278,7 +278,7 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {

const nng_tls_auth_mode mod = NANO_INTEGER(auth) ? NNG_TLS_AUTH_MODE_REQUIRED : NNG_TLS_AUTH_MODE_OPTIONAL;
R_xlen_t usefile;
nng_tls_config *cfg;
nng_tls_config *cfg = NULL;
int xc;
const char *crl, *file, *key, *pss;
SEXP xp;
Expand All @@ -287,44 +287,44 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
file = CHAR(STRING_ELT(client, 0));
usefile = XLENGTH(client);
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)))
goto exitlevel1;
goto fail;
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
goto exitlevel2;
goto fail;

if (usefile > 1) {
crl = NANO_STR_N(client, 1);
if ((xc = nng_tls_config_ca_chain(cfg, file, strncmp(crl, "", 1) ? crl : NULL)))
goto exitlevel2;
goto fail;
} else {
file = R_ExpandFileName(file);
if ((xc = nng_tls_config_ca_file(cfg, file)))
goto exitlevel2;
goto fail;
}

} else if (server != R_NilValue) {
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 exitlevel1;
goto fail;
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
goto exitlevel2;
goto fail;

if (usefile > 1) {
key = NANO_STR_N(server, 1);
if ((xc = nng_tls_config_own_cert(cfg, file, key, pss)))
goto exitlevel2;
goto fail;
} else {
file = R_ExpandFileName(file);
if ((xc = nng_tls_config_cert_key_file(cfg, file, pss)))
goto exitlevel2;
goto fail;
}

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

PROTECT(xp = R_MakeExternalPtr(cfg, nano_TlsSymbol, R_NilValue));
Expand All @@ -344,9 +344,8 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
UNPROTECT(1);
return xp;

exitlevel2:
nng_tls_config_free(cfg);
exitlevel1:
fail:
if (cfg) nng_tls_config_free(cfg);
ERROR_OUT(xc);

}
Expand Down

0 comments on commit 55c5575

Please sign in to comment.