Skip to content

Commit a022465

Browse files
committed
tidy up
verify config file in the working directory; remove redundant logic; tidy up some comments
1 parent ba4c5b6 commit a022465

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

include/reflex/input.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ class Input {
632632
return static_cast<unsigned char>(c);
633633
return EOF;
634634
}
635-
/// Copy character sequence data into buffer.
635+
/// Read and copy input character sequence data into buffer.
636636
size_t get(
637637
char *s, ///< points to the string buffer to fill with input
638638
size_t n) ///< size of buffer pointed to by s

lib/simd_avx2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ size_t simd_nlcount_avx2(const char *& b, const char *e)
7878
#endif
7979
}
8080

81-
// Partially check if valid UTF-8 encoding
81+
// Check if valid UTF-8 encoding and does not include a NUL, but pass surrogates and 3/4 byte overlongs
8282
bool simd_isutf8_avx2(const char *& b, const char *e)
8383
{
8484
#if defined(HAVE_AVX2) || defined(HAVE_AVX512BW)

src/ugrep.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,11 +2281,8 @@ struct Grep {
22812281

22822282
size_t operator()(FILE*, char *buf, size_t len)
22832283
{
2284-
if (!flag_text && !flag_hex && !flag_with_hex && flag_encoding_type != reflex::Input::file_encoding::null_data && !grep.binfile)
2285-
{
2286-
// simpler form of binary file detection without Unicode UTF-8 validation, check for NULs like GNU grep
2287-
grep.binfile = (memchr(buf, 0, len) != NULL);
2288-
}
2284+
// simpler form of binary file detection without Unicode UTF-8 validation, check for NULs like GNU grep
2285+
grep.binfile = grep.binfile || (memchr(buf, 0, len) != NULL);
22892286

22902287
return len;
22912288
}
@@ -3996,9 +3993,9 @@ struct Grep {
39963993
if (init_is_binary())
39973994
return false;
39983995
}
3999-
else if (!flag_quiet && !flag_files_with_matches && !flag_count && flag_format == NULL && !flag_text && !flag_hex && !flag_with_hex)
3996+
else if (!flag_quiet && !flag_files_with_matches && !flag_count && flag_format == NULL && !flag_text && !flag_hex && !flag_with_hex && flag_encoding_type != reflex::Input::file_encoding::null_data)
40003997
{
4001-
// not -q, -l, -c, --format, -a -X, -W: check if initial part of the file is binary
3998+
// not -q, -l, -c, --format, -a, -X, -W, -00: check if initial part of the file is binary
40023999
binfile = init_is_binary();
40034000

40044001
// detect binary input dynamically when more input is read after checking the inital input
@@ -4809,10 +4806,32 @@ static void load_config(std::list<std::pair<CNF::PATTERN,const char*>>& pattern_
48094806
std::string config_file(flag_config);
48104807
FILE *file = NULL;
48114808

4812-
if (home || fopen_smart(&file, flag_config, "r") != 0)
4809+
if (!home)
48134810
{
4814-
file = NULL;
4811+
// try opening a config file in the working directory
4812+
if (fopen_smart(&file, flag_config, "r") == 0)
4813+
{
4814+
#ifndef OS_WIN
4815+
if (file != NULL && file != stdin)
4816+
{
4817+
// check if we own this config file located in the working directory
4818+
struct stat buf;
4819+
if (fstat(fileno(file), &buf) == 0 && buf.st_uid != getuid())
4820+
{
4821+
fclose(file);
4822+
file = NULL;
4823+
}
4824+
}
4825+
#endif
4826+
}
4827+
else
4828+
{
4829+
file = NULL;
4830+
}
4831+
}
48154832

4833+
if (file == NULL)
4834+
{
48164835
// if not in the working directory, then check the home directory
48174836
if (Static::home_dir != NULL && *flag_config != '~' && *flag_config != PATHSEPCHR)
48184837
{

0 commit comments

Comments
 (0)