Skip to content

Commit e67cd63

Browse files
authored
Use own non-allocating printf in more places (#51)
* use nano_printf() in more places * use non-allocating drop-in for RE_printf() * more explicit intent
1 parent c0a8fb0 commit e67cd63

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

src/core.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ SEXP rawToChar(const unsigned char *buf, const size_t sz) {
260260
int i, j;
261261
for (i = 0, j = -1; i < sz; i++) if (buf[i]) j = i; else break;
262262
if (sz - i > 1) {
263-
REprintf("data could not be converted to a character string\n");
263+
nano_REprintf("data could not be converted to a character string\n");
264264
out = Rf_allocVector(RAWSXP, sz);
265265
memcpy(NANO_DATAPTR(out), buf, sz);
266266
return out;
@@ -415,7 +415,7 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz, SEXP hook) {
415415
}
416416
}
417417

418-
REprintf("received data could not be unserialized\n");
418+
nano_REprintf("received data could not be unserialized\n");
419419
return nano_decode(buf, sz, 8, R_NilValue);
420420

421421
resume: ;
@@ -470,7 +470,7 @@ SEXP nano_decode(unsigned char *buf, const size_t sz, const uint8_t mod, SEXP ho
470470
case 3:
471471
size = 2 * sizeof(double);
472472
if (sz % size) {
473-
REprintf("received data could not be converted to complex\n");
473+
nano_REprintf("received data could not be converted to complex\n");
474474
data = Rf_allocVector(RAWSXP, sz);
475475
} else {
476476
data = Rf_allocVector(CPLXSXP, sz / size);
@@ -479,7 +479,7 @@ SEXP nano_decode(unsigned char *buf, const size_t sz, const uint8_t mod, SEXP ho
479479
case 4:
480480
size = sizeof(double);
481481
if (sz % size) {
482-
REprintf("received data could not be converted to double\n");
482+
nano_REprintf("received data could not be converted to double\n");
483483
data = Rf_allocVector(RAWSXP, sz);
484484
} else {
485485
data = Rf_allocVector(REALSXP, sz / size);
@@ -488,7 +488,7 @@ SEXP nano_decode(unsigned char *buf, const size_t sz, const uint8_t mod, SEXP ho
488488
case 5:
489489
size = sizeof(int);
490490
if (sz % size) {
491-
REprintf("received data could not be converted to integer\n");
491+
nano_REprintf("received data could not be converted to integer\n");
492492
data = Rf_allocVector(RAWSXP, sz);
493493
} else {
494494
data = Rf_allocVector(INTSXP, sz / size);
@@ -497,7 +497,7 @@ SEXP nano_decode(unsigned char *buf, const size_t sz, const uint8_t mod, SEXP ho
497497
case 6:
498498
size = sizeof(int);
499499
if (sz % size) {
500-
REprintf("received data could not be converted to logical\n");
500+
nano_REprintf("received data could not be converted to logical\n");
501501
data = Rf_allocVector(RAWSXP, sz);
502502
} else {
503503
data = Rf_allocVector(LGLSXP, sz / size);
@@ -506,7 +506,7 @@ SEXP nano_decode(unsigned char *buf, const size_t sz, const uint8_t mod, SEXP ho
506506
case 7:
507507
size = sizeof(double);
508508
if (sz % size) {
509-
REprintf("received data could not be converted to numeric\n");
509+
nano_REprintf("received data could not be converted to numeric\n");
510510
data = Rf_allocVector(RAWSXP, sz);
511511
} else {
512512
data = Rf_allocVector(REALSXP, sz / size);

src/nanonext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ int nano_encodes(const SEXP);
246246
int nano_matcharg(const SEXP);
247247
int nano_matchargs(const SEXP);
248248

249+
void nano_REprintf(const char *);
250+
249251
SEXP rnng_advance_rng_state(void);
250252
SEXP rnng_aio_call(SEXP);
251253
SEXP rnng_aio_collect(SEXP);

src/thread.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@
2020
#define NANONEXT_IO
2121
#include "nanonext.h"
2222

23-
// messenger -------------------------------------------------------------------
23+
void nano_REprintf(const char *fmt) {
2424

25-
// # nocov start
26-
// tested interactively
25+
if (write(STDERR_FILENO, fmt, strlen(fmt))) {} ;
2726

28-
static void thread_finalizer(SEXP xptr) {
27+
}
2928

30-
if (NANO_PTR(xptr) == NULL) return;
31-
nng_thread *xp = (nng_thread *) NANO_PTR(xptr);
32-
nng_thread_destroy(xp);
29+
// messenger -------------------------------------------------------------------
3330

34-
}
31+
// # nocov start
32+
// tested interactively
3533

36-
static void nano_printf(int err, const char *fmt, ...) {
34+
static void nano_printf(const int err, const char *fmt, ...) {
3735

3836
char buf[NANONEXT_INIT_BUFSIZE];
3937
va_list arg_ptr;
@@ -42,8 +40,15 @@ static void nano_printf(int err, const char *fmt, ...) {
4240
int bytes = vsnprintf(buf, NANONEXT_INIT_BUFSIZE, fmt, arg_ptr);
4341
va_end(arg_ptr);
4442

45-
ssize_t out = write(err ? STDERR_FILENO : STDOUT_FILENO, buf, (size_t) bytes);
46-
memset(&out, 0, sizeof(ssize_t));
43+
if (write(err ? STDERR_FILENO : STDOUT_FILENO, buf, (size_t) bytes)) {};
44+
45+
}
46+
47+
static void thread_finalizer(SEXP xptr) {
48+
49+
if (NANO_PTR(xptr) == NULL) return;
50+
nng_thread *xp = (nng_thread *) NANO_PTR(xptr);
51+
nng_thread_destroy(xp);
4752

4853
}
4954

src/tls.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
165165
snprintf(issuer_name, clen, "CN=%s,O=Nanonext,C=JP", common);
166166

167167
int xc, exit = 1;
168-
if (interactive) REprintf("Generating key + certificate [ ]");
168+
if (interactive) nano_REprintf("Generating key + certificate [ ]");
169169
mbedtls_x509_crt issuer_crt;
170170
mbedtls_pk_context loaded_issuer_key;
171171
mbedtls_pk_context *issuer_key = &loaded_issuer_key;
@@ -191,18 +191,18 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
191191
mbedtls_mpi_init(&serial);
192192
#endif
193193

194-
if (interactive) REprintf("\rGenerating key + certificate [. ]");
194+
if (interactive) nano_REprintf("\rGenerating key + certificate [. ]");
195195

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

200-
if (interactive) REprintf("\rGenerating key + certificate [.. ]");
200+
if (interactive) nano_REprintf("\rGenerating key + certificate [.. ]");
201201

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

205-
if (interactive) REprintf("\rGenerating key + certificate [... ]");
205+
if (interactive) nano_REprintf("\rGenerating key + certificate [... ]");
206206

207207
if ((xc = mbedtls_pk_write_key_pem(&key, key_buf, 16000)))
208208
goto exitlevel1;
@@ -256,7 +256,7 @@ SEXP rnng_write_cert(SEXP cn, SEXP valid, SEXP inter) {
256256
SET_STRING_ELT(cstr, 0, Rf_mkChar((char *) &output_buf));
257257
SET_STRING_ELT(cstr, 1, R_BlankString);
258258

259-
if (interactive) REprintf("\rGenerating key + certificate [done]\n");
259+
if (interactive) nano_REprintf("\rGenerating key + certificate [done]\n");
260260
exit = 0;
261261

262262
exitlevel1:

0 commit comments

Comments
 (0)