commit 7307b7b3bcdd9d30da75fdafd301eb31d4a86e18
parent 660a8610887894c0b79308a1f7dcf00902fe0d4e
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 12 Apr 2023 13:26:22 +0200
Return an error if data is transmitted via stdin
Diffstat:
2 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/src/smsh.c b/src/smsh.c
@@ -26,6 +26,7 @@
#include <errno.h>
#include <unistd.h>
#include <sys/mman.h> /* mmap */
+#include <sys/stat.h> /* fstat */
/*******************************************************************************
* Helper functions
@@ -93,6 +94,18 @@ error:
goto exit;
}
+static INLINE int
+is_stdin(FILE* stream)
+{
+ struct stat stream_buf;
+ struct stat stdin_buf;
+ ASSERT(stream);
+
+ CHK(fstat(fileno(stream), &stream_buf) == 0);
+ CHK(fstat(STDIN_FILENO, &stdin_buf) == 0);
+ return stream_buf.st_dev == stdin_buf.st_dev;
+}
+
static res_T
load_stream(struct smsh* smsh, FILE* stream, const char* stream_name)
{
@@ -102,6 +115,13 @@ load_stream(struct smsh* smsh, FILE* stream, const char* stream_name)
res_T res = RES_OK;
ASSERT(smsh && stream && stream_name);
+ if(is_stdin(stream)) {
+ log_err(smsh, "%s: the data cannot be load from the standard input\n",
+ stream_name);
+ res = RES_IO_ERR;
+ goto error;
+ }
+
reset_smsh(smsh);
/* Read file header */
diff --git a/src/test_smsh_load.c b/src/test_smsh_load.c
@@ -201,6 +201,9 @@ test_load_fail(struct smsh* smsh)
rewind(fp);
CHK(smsh_load_stream(smsh, fp, NULL) == RES_IO_ERR);
fclose(fp);
+
+ /* Wrong stream */
+ CHK(smsh_load_stream(smsh, stdin, "stdin") == RES_IO_ERR);
}
static void