Skip to content

Commit

Permalink
catch one error path
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 16, 2025
1 parent 327873c commit 764f86d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ static SEXP rawToChar(const unsigned char *buf, const size_t sz) {
SEXP out;
int i, j;
for (i = 0, j = -1; i < sz; i++) if (buf[i]) j = i; else break;
if (sz - i > 1) {
Rf_error("data could not be converted to a character string");
}
if (sz - i > 1) { ERROR_CONVERT(buf); }

PROTECT(out = Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(out, 0, Rf_mkCharLenCE((const char *) buf, j + 1, CE_NATIVE));
Expand Down Expand Up @@ -367,7 +365,7 @@ SEXP secretbase_base64enc(SEXP x, SEXP convert) {
unsigned char *buf = R_Calloc(olen, unsigned char);
xc = mbedtls_base64_encode(buf, olen, &olen, hash.buf, hash.cur);
NANO_FREE(hash);
CHECK_ERROR(xc);
CHECK_ERROR(xc, buf);

if (conv) {
out = rawToChar(buf, olen);
Expand Down Expand Up @@ -410,7 +408,7 @@ SEXP secretbase_base64dec(SEXP x, SEXP convert) {
Rf_error("input is not valid base64");
unsigned char *buf = R_Calloc(olen, unsigned char);
xc = mbedtls_base64_decode(buf, olen, &olen, inbuf, inlen);
CHECK_ERROR(xc);
CHECK_ERROR(xc, buf);

switch (conv) {
case 0:
Expand Down
4 changes: 3 additions & 1 deletion src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ Rf_error("'file' must be a character string")
(x)->len = 0; \
(x)->cur = sz
#define NANO_FREE(x) if (x.len) R_Free(x.buf)
#define CHECK_ERROR(x) if (x) { R_Free(buf); \
#define CHECK_ERROR(x, y) if (x) { R_Free(y); \
Rf_error("write buffer insufficient"); }
#define ERROR_OUT(x) if (x->len) R_Free(x->buf); \
Rf_error("serialization exceeds max length of raw vector")
#define ERROR_CONVERT(x) R_Free(x); \
Rf_error("data could not be converted to a character string")
#define ERROR_FOPEN(x) Rf_error("file not found or no read permission at '%s'", x)
#define ERROR_FREAD(x) Rf_error("file read error at '%s'", x)

Expand Down

0 comments on commit 764f86d

Please sign in to comment.