Merge remote-tracking branch 'origin/topic/jsiwek/util-fixes'

* origin/topic/jsiwek/util-fixes:
  Fix race condition in ensure_dir()
  Fix tokenize_string() to work with delimiters of length > 1
This commit is contained in:
Johanna Amann 2020-07-17 22:32:42 +00:00
commit a0c0236fb1
3 changed files with 29 additions and 21 deletions

View file

@ -1,4 +1,13 @@
3.2.0-dev.911 | 2020-07-17 22:32:42 +0000
* Fix race condition in ensure_dir()
If something else created the dir between the stat() and mkdir(),
it previously reported that as a failure. (Jon Siwek, Corelight)
* Fix tokenize_string() to work with delimiters of length > 1. (Jon Siwek, Corelight)
3.2.0-dev.908 | 2020-07-17 10:19:07 +0000
* GH-734: Improve handling of lambdas that escape enclosing frame.

View file

@ -1 +1 @@
3.2.0-dev.908
3.2.0-dev.911

View file

@ -901,31 +901,26 @@ bool ensure_intermediate_dirs(const char* dirname)
bool ensure_dir(const char *dirname)
{
if ( mkdir(dirname, 0700) == 0 )
return true;
auto mkdir_errno = errno;
struct stat st;
if ( stat(dirname, &st) < 0 )
{
if ( errno != ENOENT )
{
reporter->Warning("can't stat directory %s: %s",
dirname, strerror(errno));
return false;
}
if ( mkdir(dirname, 0700) < 0 )
{
reporter->Warning("can't create directory %s: %s",
dirname, strerror(errno));
return false;
}
}
else if ( ! S_ISDIR(st.st_mode) )
if ( stat(dirname, &st) == -1 )
{
reporter->Warning("%s exists but is not a directory", dirname);
// Show the original failure reason for mkdir() since nothing's there
// or we can't even tell what is now.
reporter->Warning("can't create directory %s: %s",
dirname, strerror(mkdir_errno));
return false;
}
return true;
if ( S_ISDIR(st.st_mode) )
return true;
reporter->Warning("%s exists but is not a directory", dirname);
return false;
}
bool is_dir(const std::string& path)
@ -1530,6 +1525,10 @@ TEST_CASE("util tokenize_string")
auto svs = tokenize_string("one,two,three,four,", ',');
std::vector<std::string_view> expect{"one", "two", "three", "four", ""};
CHECK(svs == expect);
auto letters = tokenize_string("a--b--c--d", "--");
CHECK(*letters == vector<string>({ "a", "b", "c", "d" }));
delete letters;
}
vector<string>* tokenize_string(std::string_view input, std::string_view delim,
@ -1546,7 +1545,7 @@ vector<string>* tokenize_string(std::string_view input, std::string_view delim,
{
++found;
rval->emplace_back(input.substr(pos, n - pos));
pos = n + 1;
pos = n + delim.size();
if ( limit && found == limit )
break;