From ce105dcf099699e2446f1aa76072283b75194fa1 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sun, 7 May 2017 17:04:53 -0400 Subject: [PATCH 1/5] Address #391. Tested on macOS and Win10. - Add a check upon opening a file for validity of the file. - Add a new message to indicate that the path is not a file. --- include/tidyenum.h | 1 + src/config.c | 2 +- src/language_en.h | 1 + src/mappedio.c | 2 +- src/message.c | 4 ++-- src/message.h | 2 +- src/tidylib.c | 20 +++++++++++++++----- 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/tidyenum.h b/include/tidyenum.h index 565eee9..b4d3410 100644 --- a/include/tidyenum.h +++ b/include/tidyenum.h @@ -101,6 +101,7 @@ extern "C" { */ #define FOREACH_MSG_MISC(FN) \ /** File can't be opened */ FN(FILE_CANT_OPEN) \ +/** Not a file */ FN(FILE_NOT_FILE) \ /** line %d column %d */ FN(LINE_COLUMN_STRING) \ /** Document content looks like %s */ FN(STRING_CONTENT_LOOKS) \ /** discarding */ FN(STRING_DISCARDING) \ diff --git a/src/config.c b/src/config.c index 0026dc9..f1b62d0 100644 --- a/src/config.c +++ b/src/config.c @@ -808,7 +808,7 @@ int TY_(ParseConfigFileEnc)( TidyDocImpl* doc, ctmbstr file, ctmbstr charenc ) if ( fin == NULL || enc < 0 ) { - TY_(FileError)( doc, fname, TidyConfig ); + TY_(FileError)( doc, fname, TidyConfig, FILE_CANT_OPEN ); return -1; } else diff --git a/src/language_en.h b/src/language_en.h index 364f69b..aafef19 100644 --- a/src/language_en.h +++ b/src/language_en.h @@ -1545,6 +1545,7 @@ static languageDefinition language_en = { whichPluralForm_en, { ** @rename enum generator FOREACH_MSG_MISC ********************************************/ { FILE_CANT_OPEN, 0, "Can't open \"%s\"\n" }, + { FILE_NOT_FILE, 0, "\"%s\" is not a file!\n" }, { LINE_COLUMN_STRING, 0, "line %d column %d - " }, { STRING_CONTENT_LOOKS, 0, "Document content looks like %s" }, {/* For example, "discarding invalid UTF-16 surrogate pair" */ diff --git a/src/mappedio.c b/src/mappedio.c index f80c978..e5da343 100644 --- a/src/mappedio.c +++ b/src/mappedio.c @@ -326,7 +326,7 @@ int TY_(DocParseFileWithMappedFile)( TidyDocImpl* doc, ctmbstr filnam ) { TY_(freeStreamIn)( in ); } else /* Error message! */ - TY_(FileError)( doc, filnam, TidyError ); + TY_(FileError)( doc, filnam, TidyError, FILE_CANT_OPEN ); return status; } diff --git a/src/message.c b/src/message.c index 2c5587e..8efa9f2 100755 --- a/src/message.c +++ b/src/message.c @@ -459,9 +459,9 @@ void TY_(ReportFatal)( TidyDocImpl* doc, Node *element, Node *node, uint code) *********************************************************************/ -void TY_(FileError)( TidyDocImpl* doc, ctmbstr file, TidyReportLevel level ) +void TY_(FileError)( TidyDocImpl* doc, ctmbstr file, TidyReportLevel level, uint code ) { - TidyMessageImpl *message = TY_(tidyMessageCreate)( doc, FILE_CANT_OPEN, level, file); + TidyMessageImpl *message = TY_(tidyMessageCreate)( doc, code, level, file); messageOut( message ); } diff --git a/src/message.h b/src/message.h index 25a96f0..82a3622 100644 --- a/src/message.h +++ b/src/message.h @@ -59,7 +59,7 @@ void TY_(ReportFatal)(TidyDocImpl* doc, Node* element, Node* node, uint code); /** @{ */ -void TY_(FileError)( TidyDocImpl* doc, ctmbstr file, TidyReportLevel level ); +void TY_(FileError)( TidyDocImpl* doc, ctmbstr file, TidyReportLevel level, uint code ); void TY_(ReportAttrError)( TidyDocImpl* doc, Node* node, AttVal* av, uint code ); void TY_(ReportBadArgument)( TidyDocImpl* doc, ctmbstr option ); void TY_(ReportEncodingError)(TidyDocImpl* doc, uint code, uint c, Bool discarded); diff --git a/src/tidylib.c b/src/tidylib.c index 62afefe..25343d5 100755 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -903,7 +903,7 @@ FILE* TIDY_CALL tidySetErrorFile( TidyDoc tdoc, ctmbstr errfilnam ) return errout; } else /* Emit message to current error sink */ - TY_(FileError)( impl, errfilnam, TidyError ); + TY_(FileError)( impl, errfilnam, TidyError, FILE_CANT_OPEN ); } return NULL; } @@ -1068,11 +1068,21 @@ int TIDY_CALL tidyParseSource( TidyDoc tdoc, TidyInputSource* source ) int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam ) { + int status = -ENOENT; + FILE* fin = fopen( filnam, "r+" ); + + if ( !fin ) + { + TY_(FileError)( doc, filnam, TidyError, FILE_NOT_FILE ); + return status; + } + #ifdef _WIN32 return TY_(DocParseFileWithMappedFile)( doc, filnam ); #else - int status = -ENOENT; - FILE* fin = fopen( filnam, "rb" ); + + fclose( fin ); + fin = fopen( filnam, "rb" ); #if PRESERVE_FILE_TIMES struct stat sbuf = {0}; @@ -1099,7 +1109,7 @@ int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam ) TY_(freeStreamIn)(in); } else /* Error message! */ - TY_(FileError)( doc, filnam, TidyError ); + TY_(FileError)( doc, filnam, TidyError, FILE_CANT_OPEN ); return status; #endif } @@ -1213,7 +1223,7 @@ int tidyDocSaveFile( TidyDocImpl* doc, ctmbstr filnam ) #endif /* PRESERVFILETIMES */ } if ( status < 0 ) /* Error message! */ - TY_(FileError)( doc, filnam, TidyError ); + TY_(FileError)( doc, filnam, TidyError, FILE_CANT_OPEN ); return status; } From f7e7554c95d31cd8adbc47c66084f671161bbcb4 Mon Sep 17 00:00:00 2001 From: Geoff McLane Date: Tue, 9 May 2017 19:24:20 +0200 Subject: [PATCH 2/5] Close the file before the _WIN32 switch --- src/tidylib.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tidylib.c b/src/tidylib.c index 25343d5..fad55e6 100755 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -1077,11 +1077,12 @@ int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam ) return status; } + fclose( fin ); + #ifdef _WIN32 return TY_(DocParseFileWithMappedFile)( doc, filnam ); #else - fclose( fin ); fin = fopen( filnam, "rb" ); #if PRESERVE_FILE_TIMES From 66bed8b9a05a1f478412976a90c9be8f366c7c66 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Thu, 11 May 2017 15:25:46 -0400 Subject: [PATCH 3/5] Bumped to 5.5.21 for #391 fix. --- version.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.txt b/version.txt index fa309cc..1fdb764 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ -5.5.20 +5.5.21 2017.05.07 From a399725a1e0d4b15914f8ee9ca3f4475ad2458a6 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sat, 13 May 2017 11:39:13 -0400 Subject: [PATCH 4/5] Fixed ParseAutoBool error. --- src/config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.c b/src/config.c index a35becc..5960137 100644 --- a/src/config.c +++ b/src/config.c @@ -593,7 +593,7 @@ TidyTriState TY_(_cfgGetAutoBool)( TidyDocImpl* doc, TidyOptionId optId ) ulong val = TY_(_cfgGet)( doc, optId ); const TidyOptionImpl* opt = &option_defs[ optId ]; assert( opt && opt->type == TidyInteger - && opt->parser == ParseAutoBool ); + && opt->parser == ParsePickList ); return (TidyTriState) val; } From 86338b2634c6b3c70019afd11fb5f360a19ee972 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Sat, 13 May 2017 19:51:28 -0400 Subject: [PATCH 5/5] Bumped to 5.5.22 for internal change. --- version.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/version.txt b/version.txt index 1fdb764..45b1d41 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ -5.5.21 -2017.05.07 +5.5.22 +2017.05.13