From a191697bd6d8c5ba9f0837e8099fde4625fbcdcb Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Sun, 28 Jan 2018 15:07:42 +0100 Subject: [PATCH] safeguard opening of file for debug output: may be not allowed if caller is not started in admin mode. --- src/frontend/inp.c | 64 ++++++++++++++++++++++++------------------- src/frontend/inpcom.c | 40 +++++++++++++++------------ 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/src/frontend/inp.c b/src/frontend/inp.c index 1ab981dc6..4affda156 100644 --- a/src/frontend/inp.c +++ b/src/frontend/inp.c @@ -619,21 +619,25 @@ inp_spsource(FILE *fp, bool comfile, char *filename, bool intfile) if (ft_ngdebug) { /*debug: print into file*/ FILE *fdo = fopen("debug-out2.txt", "w"); - struct card *t = NULL; - fprintf(fdo, "**************** uncommented deck **************\n\n"); - /* always print first line */ - fprintf(fdo, "%6d %6d %s\n", deck->linenum_orig, deck->linenum, deck->line); - /* here without out-commented lines */ - for (t = deck->nextcard; t; t = t->nextcard) { - if (*(t->line) == '*') - continue; - fprintf(fdo, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); + if (fdo) { + struct card *t = NULL; + fprintf(fdo, "**************** uncommented deck **************\n\n"); + /* always print first line */ + fprintf(fdo, "%6d %6d %s\n", deck->linenum_orig, deck->linenum, deck->line); + /* here without out-commented lines */ + for (t = deck->nextcard; t; t = t->nextcard) { + if (*(t->line) == '*') + continue; + fprintf(fdo, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); + } + fprintf(fdo, "\n****************** complete deck ***************\n\n"); + /* now completely */ + for (t = deck; t; t = t->nextcard) + fprintf(fdo, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); + fclose(fdo); } - fprintf(fdo, "\n****************** complete deck ***************\n\n"); - /* now completely */ - for (t = deck; t; t = t->nextcard) - fprintf(fdo, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); - fclose(fdo); + else + fprintf(stderr, "Warning: Cannot open file debug-out2.txt for saving debug info\n"); } for (dd = deck; dd; dd = dd->nextcard) { /* get csparams and create vectors, being @@ -761,21 +765,25 @@ inp_spsource(FILE *fp, bool comfile, char *filename, bool intfile) if (ft_ngdebug) { /*debug: print into file*/ FILE *fdo = fopen("debug-out3.txt", "w"); - struct card *t = NULL; - fprintf(fdo, "**************** uncommented deck **************\n\n"); - /* always print first line */ - fprintf(fdo, "%6d %6d %s\n", deck->linenum_orig, deck->linenum, deck->line); - /* here without out-commented lines */ - for (t = deck->nextcard; t; t = t->nextcard) { - if (*(t->line) == '*') - continue; - fprintf(fdo, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); + if (fdo) { + struct card *t = NULL; + fprintf(fdo, "**************** uncommented deck **************\n\n"); + /* always print first line */ + fprintf(fdo, "%6d %6d %s\n", deck->linenum_orig, deck->linenum, deck->line); + /* here without out-commented lines */ + for (t = deck->nextcard; t; t = t->nextcard) { + if (*(t->line) == '*') + continue; + fprintf(fdo, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); + } + fprintf(fdo, "\n****************** complete deck ***************\n\n"); + /* now completely */ + for (t = deck; t; t = t->nextcard) + fprintf(fdo, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); + fclose(fdo); } - fprintf(fdo, "\n****************** complete deck ***************\n\n"); - /* now completely */ - for (t = deck; t; t = t->nextcard) - fprintf(fdo, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); - fclose(fdo); + else + fprintf(stderr, "Warning: Cannot open file debug-out3.txt for saving debug info\n"); } /* Now the circuit is defined, so generate the parse trees */ diff --git a/src/frontend/inpcom.c b/src/frontend/inpcom.c index 5943f133c..d3eee5613 100644 --- a/src/frontend/inpcom.c +++ b/src/frontend/inpcom.c @@ -620,26 +620,30 @@ inp_readall(FILE *fp, char *dir_name, bool comfile, bool intfile, bool *expr_w_t } if (ft_ngdebug) { - /*debug: print into file*/ FILE *fd = fopen("debug-out.txt", "w"); - struct card *t; - fprintf(fd, "**************** uncommented deck **************\n\n"); - /* always print first line */ - fprintf(fd, "%6d %6d %s\n", cc->linenum_orig, cc->linenum, cc->line); - /* here without out-commented lines */ - for (t = cc->nextcard; t; t = t->nextcard) { - if (*(t->line) == '*') - continue; - fprintf(fd, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); - } - fprintf(fd, "\n****************** complete deck ***************\n\n"); - /* now completely */ - for (t = cc; t; t = t->nextcard) - fprintf(fd, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); - fclose(fd); + if (fd) { + /*debug: print into file*/ + struct card *t; + fprintf(fd, "**************** uncommented deck **************\n\n"); + /* always print first line */ + fprintf(fd, "%6d %6d %s\n", cc->linenum_orig, cc->linenum, cc->line); + /* here without out-commented lines */ + for (t = cc->nextcard; t; t = t->nextcard) { + if (*(t->line) == '*') + continue; + fprintf(fd, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); + } + fprintf(fd, "\n****************** complete deck ***************\n\n"); + /* now completely */ + for (t = cc; t; t = t->nextcard) + fprintf(fd, "%6d %6d %s\n", t->linenum_orig, t->linenum, t->line); + fclose(fd); - fprintf(stdout, "max line length %d, max subst. per line %d, number of lines %d\n", - (int) max_line_length, no_braces, dynmaxline); + fprintf(stdout, "max line length %d, max subst. per line %d, number of lines %d\n", + (int)max_line_length, no_braces, dynmaxline); + } + else + fprintf(stderr, "Warning: Cannot open file debug-out.txt for saving debug info\n"); } }