aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGavin Howard <gavin@yzena.com>2021-07-23 13:02:33 -0600
committerGavin Howard <gavin@yzena.com>2021-07-23 13:02:33 -0600
commitde7f0b83b620958cd325f95a472efda2a1912092 (patch)
tree78ca66fab6c53beae33a219fb6d794b9d0162ae1 /src
parent7efba6d83b9605e4f932d8a3e41ac878be8714eb (diff)
downloadplatform_external_bc-de7f0b83b620958cd325f95a472efda2a1912092.tar.gz
platform_external_bc-de7f0b83b620958cd325f95a472efda2a1912092.tar.bz2
platform_external_bc-de7f0b83b620958cd325f95a472efda2a1912092.zip
Add the check for binary files back in
This does not add it in as it was; it only checks for nul bytes. This is to prevent problems treating the data as a string. Signed-off-by: Gavin Howard <gavin@yzena.com>
Diffstat (limited to 'src')
-rw-r--r--src/data.c3
-rw-r--r--src/read.c24
2 files changed, 26 insertions, 1 deletions
diff --git a/src/data.c b/src/data.c
index decd484f..81cb5cf5 100644
--- a/src/data.c
+++ b/src/data.c
@@ -198,7 +198,7 @@ const uchar bc_err_ids[] = {
BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
- BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
+ BC_ERR_IDX_PARSE,
#endif // BC_ENABLED
};
@@ -215,6 +215,7 @@ const char* const bc_err_msgs[] = {
"memory allocation failed",
"I/O error",
"cannot open file: %s",
+ "file is not text: %s",
"path is a directory: %s",
"bad command-line option: \"%s\"",
"option requires an argument: '%c' (\"%s\")",
diff --git a/src/read.c b/src/read.c
index d2b3287e..aa906c7e 100644
--- a/src/read.c
+++ b/src/read.c
@@ -72,6 +72,22 @@ static int bc_read_open(const char* path, int mode) {
return fd;
}
+/**
+ * Returns true if the buffer data is non-text.
+ * @param buf The buffer to test.
+ * @param size The size of the buffer.
+ */
+static bool bc_read_binary(const char *buf, size_t size) {
+
+ size_t i;
+
+ for (i = 0; i < size; ++i) {
+ if (BC_ERR(BC_READ_BIN_CHAR(buf[i]))) return true;
+ }
+
+ return false;
+}
+
bool bc_read_buf(BcVec *vec, char *buf, size_t *buf_len) {
char *nl;
@@ -207,6 +223,9 @@ BcStatus bc_read_line(BcVec *vec, const char *prompt) {
s = bc_read_chars(vec, prompt);
#endif // BC_ENABLE_HISTORY
+ if (BC_ERR(bc_read_binary(vec->v, vec->len - 1)))
+ bc_verr(BC_ERR_FATAL_BIN_FILE, bc_program_stdin_name);
+
return s;
}
@@ -254,6 +273,11 @@ char* bc_read_file(const char *path) {
// Got to have a nul byte.
buf[size] = '\0';
+ if (BC_ERR(bc_read_binary(buf, size))) {
+ e = BC_ERR_FATAL_BIN_FILE;
+ goto read_err;
+ }
+
close(fd);
return buf;