Skip to content

Commit

Permalink
adds file read error detection
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 25, 2024
1 parent 96caf95 commit daea8de
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,18 @@ static SEXP secretbase_sha3_impl(const SEXP x, const SEXP bits,

const char *filepath = R_ExpandFileName(CHAR(STRING_ELT(x, 0)));
unsigned char buf[SB_BUF_SIZE];
FILE *fp;
size_t cur;

FILE *fp = fopen(filepath, "rb");
if (fp == NULL)
if ((fp = fopen(filepath, "rb")) == NULL)
Rf_error("file not found or no read permission");
while ((cur = fread(buf, 1, sizeof(buf), fp))) {
mbedtls_sha3_update(&ctx, buf, cur);
}
if (ferror(fp)) {
fclose(fp);
Rf_error("file read error");
}
fclose(fp);

} else {
Expand Down
1 change: 1 addition & 0 deletions tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ hash_func <- function(file, string) {
}
test_equal(hash_func(tempfile(), "secret base"), "a721d57570e7ce366adee2fccbe9770723c6e3622549c31c7cab9dbb4a795520")
test_error(hash_func("", ""), "file not found or no read permission")
if (Sys.info()[["sysname"]] == "Linux") test_error(sha3file("/proc/self/mem"), "file read error")

0 comments on commit daea8de

Please sign in to comment.