Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit cc97bca

Browse files
committed
Treat filenames starting with hyphen correctly
In places we were a bit sloppy with the file name check and names only starting with '-' would be interpreted as the magic "-" filename for stdin/stdout. Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
1 parent a1b2fe4 commit cc97bca

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

main.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ int retry = 10;
8181
char exec_flag = 0;
8282
uint32_t execute = 0;
8383
char init_flag = 1;
84+
int use_stdinout = 0;
8485
char force_binary = 0;
8586
char reset_flag = 0;
8687
char *filename;
@@ -239,7 +240,7 @@ int main(int argc, char* argv[]) {
239240
sigaction(SIGINT, &sigIntHandler, NULL);
240241
#endif
241242

242-
if ((action == ACT_READ) && filename[0] == '-') {
243+
if (action == ACT_READ && use_stdinout) {
243244
diag = stderr;
244245
}
245246

@@ -487,7 +488,7 @@ int main(int argc, char* argv[]) {
487488
max_rlen = max_rlen < max_wlen ? max_rlen : max_wlen;
488489

489490
/* Assume data from stdin is whole device */
490-
if (filename[0] == '-' && filename[1] == '\0')
491+
if (use_stdinout)
491492
size = end - start;
492493
else
493494
size = parser->size(p_st);
@@ -521,7 +522,7 @@ int main(int argc, char* argv[]) {
521522
goto close;
522523

523524
if (len == 0) {
524-
if (filename[0] == '-') {
525+
if (use_stdinout) {
525526
break;
526527
} else {
527528
fprintf(stderr, "Failed to read input file\n");
@@ -680,7 +681,8 @@ int parse_options(int argc, char *argv[])
680681
}
681682
action = (c == 'r') ? ACT_READ : ACT_WRITE;
682683
filename = optarg;
683-
if (filename[0] == '-') {
684+
if (filename[0] == '-' && filename[1] == '\0') {
685+
use_stdinout = 1;
684686
force_binary = 1;
685687
}
686688
break;

parsers/binary.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void* binary_init() {
3939
parser_err_t binary_open(void *storage, const char *filename, const char write) {
4040
binary_t *st = storage;
4141
if (write) {
42-
if (filename[0] == '-')
42+
if (filename[0] == '-' && filename[1] == '\0')
4343
st->fd = 1;
4444
else
4545
st->fd = open(
@@ -57,7 +57,7 @@ parser_err_t binary_open(void *storage, const char *filename, const char write)
5757
);
5858
st->stat.st_size = 0;
5959
} else {
60-
if (filename[0] == '-') {
60+
if (filename[0] == '-' && filename[1] == '\0') {
6161
st->fd = 0;
6262
} else {
6363
if (stat(filename, &st->stat) != 0)

0 commit comments

Comments
 (0)