Merge remote-tracking branch 'origin/topic/timw/util-types'

* origin/topic/timw/util-types:
  Remove some unused #includes from spicy code
  Remove using util.h in various headers in favor of util-types.h
  Move type definitions/aliases from util.h to a separate file
This commit is contained in:
Tim Wojtulewicz 2025-06-11 11:12:18 -07:00
commit 0a8149c185
23 changed files with 177 additions and 131 deletions

View file

@ -1,3 +1,11 @@
8.0.0-dev.423 | 2025-06-11 11:12:18 -0700
* Remove some unused #includes from spicy code (Tim Wojtulewicz, Corelight)
* Remove using util.h in various headers in favor of util-types.h (Tim Wojtulewicz, Corelight)
* Move type definitions/aliases from util.h to a separate file (Tim Wojtulewicz, Corelight)
8.0.0-dev.419 | 2025-06-11 09:00:46 -0700 8.0.0-dev.419 | 2025-06-11 09:00:46 -0700
* Bugfix: AsString() on an EnumVal will segfault (Christian Kreibich, Corelight) * Bugfix: AsString() on an EnumVal will segfault (Christian Kreibich, Corelight)

View file

@ -1 +1 @@
8.0.0-dev.419 8.0.0-dev.423

View file

@ -318,6 +318,7 @@ set(MAIN_SRCS
digest.cc digest.cc
net_util.cc net_util.cc
util.cc util.cc
util-types.cc
module_util.cc module_util.cc
zeek-affinity.cc zeek-affinity.cc
zeek-setup.cc zeek-setup.cc

View file

@ -9,7 +9,7 @@
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/ZeekString.h" // for byte_vec #include "zeek/ZeekString.h" // for byte_vec
#include "zeek/util.h" // for zeek_int_t #include "zeek/util-types.h"
namespace zeek { namespace zeek {

View file

@ -9,7 +9,7 @@
#include "zeek/ZeekArgs.h" #include "zeek/ZeekArgs.h"
#include "zeek/analyzer/Analyzer.h" #include "zeek/analyzer/Analyzer.h"
#include "zeek/iosource/IOSource.h" #include "zeek/iosource/IOSource.h"
#include "zeek/util.h" #include "zeek/util-types.h"
namespace zeek { namespace zeek {

View file

@ -8,7 +8,7 @@
#include "zeek/IPAddr.h" #include "zeek/IPAddr.h"
#include "zeek/Reassem.h" #include "zeek/Reassem.h"
#include "zeek/Timer.h" #include "zeek/Timer.h"
#include "zeek/util.h" // for zeek_uint_t #include "zeek/util-types.h"
namespace zeek { namespace zeek {

View file

@ -22,7 +22,7 @@
#include <unistd.h> #include <unistd.h>
#include <cstdlib> #include <cstdlib>
#include "zeek/util.h" // for zeek_int_t #include "zeek/util-types.h" // for zeek_int_t
// to allow md5_hmac_bif access to the hmac seed // to allow md5_hmac_bif access to the hmac seed
#include "zeek/ZeekArgs.h" #include "zeek/ZeekArgs.h"

View file

@ -2,9 +2,14 @@
#pragma once #pragma once
#include "zeek/Type.h" #include "zeek/util-types.h"
namespace zeek::detail { namespace zeek {
class Type;
class Val;
namespace detail {
inline bool double_to_count_would_overflow(double v) { return v < 0.0 || v > static_cast<double>(UINT64_MAX); } inline bool double_to_count_would_overflow(double v) { return v < 0.0 || v > static_cast<double>(UINT64_MAX); }
@ -18,4 +23,5 @@ inline bool count_to_int_would_overflow(zeek_uint_t v) { return v > INT64_MAX; }
extern bool would_overflow(const zeek::Type* from_type, const zeek::Type* to_type, const Val* val); extern bool would_overflow(const zeek::Type* from_type, const zeek::Type* to_type, const Val* val);
} // namespace zeek::detail } // namespace detail
} // namespace zeek

View file

@ -5,7 +5,7 @@
#include <cstring> #include <cstring>
#include <string> #include <string>
#include "zeek/util.h" // for zeek_int_t #include "zeek/util-types.h"
namespace zeek { namespace zeek {

View file

@ -4,7 +4,6 @@
#include "zeek/Tag.h" #include "zeek/Tag.h"
#include "zeek/plugin/Component.h" #include "zeek/plugin/Component.h"
#include "zeek/util.h"
namespace zeek { namespace zeek {

View file

@ -1,7 +1,7 @@
%extern{ %extern{
#include "zeek/3rdparty/ConvertUTF.h" #include "zeek/3rdparty/ConvertUTF.h"
#include "zeek/binpac_zeek.h" #include "zeek/binpac_zeek.h"
#include "zeek/util.h" #include "zeek/util-types.h"
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
#include "zeek/Val.h" #include "zeek/Val.h"
#include "zeek/RunState.h" #include "zeek/RunState.h"

View file

@ -10,7 +10,7 @@
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include "zeek/util.h" #include "zeek/util.h" // for util::bytetohex
// Required buffer size for an MD5 digest. // Required buffer size for an MD5 digest.
#define ZEEK_MD5_DIGEST_LENGTH 16 #define ZEEK_MD5_DIGEST_LENGTH 16

View file

@ -5,7 +5,7 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include "zeek/util.h" #include "zeek/util-types.h"
extern "C" { extern "C" {
#include <pcap.h> #include <pcap.h>

View file

@ -6,7 +6,6 @@
#include "zeek/Tag.h" #include "zeek/Tag.h"
#include "zeek/plugin/Component.h" #include "zeek/plugin/Component.h"
#include "zeek/util.h"
namespace zeek::packet_analysis { namespace zeek::packet_analysis {

View file

@ -8,7 +8,7 @@
#include "zeek/Attr.h" #include "zeek/Attr.h"
#include "zeek/ID.h" #include "zeek/ID.h"
#include "zeek/util.h" #include "zeek/util-types.h"
namespace zeek::detail { namespace zeek::detail {

View file

@ -9,11 +9,13 @@
#pragma once #pragma once
#include <memory>
#include <string>
#include <vector> #include <vector>
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/script_opt/ZAM/Profile.h" #include "zeek/script_opt/ZAM/Profile.h"
#include "zeek/util.h" #include "zeek/util-types.h"
namespace zeek { namespace zeek {

View file

@ -6,7 +6,6 @@
#pragma once #pragma once
#include <limits>
#include <optional> #include <optional>
#include <ostream> #include <ostream>
#include <string> #include <string>

View file

@ -6,7 +6,6 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <tuple>
#include <utility> #include <utility>
#include <vector> #include <vector>

View file

@ -6,7 +6,7 @@
#include <vector> #include <vector>
#include "zeek/Span.h" #include "zeek/Span.h"
#include "zeek/util.h" #include "zeek/util-types.h"
namespace zeek::telemetry { namespace zeek::telemetry {

78
src/util-types.cc Normal file
View file

@ -0,0 +1,78 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include "zeek/util-types.h"
#include "zeek/Reporter.h"
#include "zeek/3rdparty/doctest.h"
namespace zeek::util {
namespace detail {
void SafePathOp::CheckValid(const char* op_result, const char* path, bool error_aborts) {
if ( op_result ) {
result = op_result;
error = false;
}
else {
if ( error_aborts )
reporter->InternalError("Path operation failed on %s: %s", path ? path : "<null>", strerror(errno));
else
error = true;
}
}
TEST_CASE("util path ops") {
#ifdef _MSC_VER
// TODO: adapt these tests to Windows paths
#else
SUBCASE("SafeDirname") {
SafeDirname d("/this/is/a/path", false);
CHECK(d.result == "/this/is/a");
SafeDirname d2("invalid", false);
CHECK(d2.result == ".");
SafeDirname d3("./filename", false);
CHECK(d2.result == ".");
}
SUBCASE("SafeBasename") {
SafeBasename b("/this/is/a/path", false);
CHECK(b.result == "path");
CHECK(! b.error);
SafeBasename b2("justafile", false);
CHECK(b2.result == "justafile");
CHECK(! b2.error);
}
#endif
}
} // namespace detail
SafeDirname::SafeDirname(const char* path, bool error_aborts) : SafePathOp() { DoFunc(path ? path : "", error_aborts); }
SafeDirname::SafeDirname(const std::string& path, bool error_aborts) : SafePathOp() { DoFunc(path, error_aborts); }
void SafeDirname::DoFunc(const std::string& path, bool error_aborts) {
char* tmp = copy_string(path.c_str());
CheckValid(dirname(tmp), tmp, error_aborts);
delete[] tmp;
}
SafeBasename::SafeBasename(const char* path, bool error_aborts) : SafePathOp() {
DoFunc(path ? path : "", error_aborts);
}
SafeBasename::SafeBasename(const std::string& path, bool error_aborts) : SafePathOp() { DoFunc(path, error_aborts); }
void SafeBasename::DoFunc(const std::string& path, bool error_aborts) {
char* tmp = copy_string(path.c_str());
CheckValid(basename(tmp), tmp, error_aborts);
delete[] tmp;
}
} // namespace zeek::util

66
src/util-types.h Normal file
View file

@ -0,0 +1,66 @@
// See the file "COPYING" in the main distribution directory for copyright.
#pragma once
#include <cstdint>
#include <string>
#include "zeek/3rdparty/nonstd/expected.hpp"
#include "zeek/Span.h"
// These two types are not namespaced intentionally.
using zeek_int_t = int64_t;
using zeek_uint_t = uint64_t;
namespace zeek {
// Type aliases for nonstd::expected/nonstd::unexpected. These should be switched to use
// the std:: versions once we switch to C++20.
template<typename T, typename E>
using expected = nonstd::expected<T, E>;
template<typename E>
using unexpected = nonstd::unexpected<E>;
// Byte buffer types used by serialization code in storage and cluster.
using byte_buffer = std::vector<std::byte>;
using byte_buffer_span = Span<const std::byte>;
namespace util {
namespace detail {
/**
* Wrapper class for functions like dirname(3) or basename(3) that won't
* modify the path argument and may optionally abort execution on error.
*/
class SafePathOp {
public:
std::string result;
bool error = false;
protected:
void CheckValid(const char* result, const char* path, bool error_aborts);
};
} // namespace detail
class SafeDirname : public detail::SafePathOp {
public:
explicit SafeDirname(const char* path, bool error_aborts = true);
explicit SafeDirname(const std::string& path, bool error_aborts = true);
private:
void DoFunc(const std::string& path, bool error_aborts = true);
};
class SafeBasename : public detail::SafePathOp {
public:
explicit SafeBasename(const char* path, bool error_aborts = true);
explicit SafeBasename(const std::string& path, bool error_aborts = true);
private:
void DoFunc(const std::string& path, bool error_aborts = true);
};
} // namespace util
} // namespace zeek

View file

@ -531,19 +531,6 @@ FILE* open_package(string& path, const string& mode) {
return nullptr; return nullptr;
} }
void SafePathOp::CheckValid(const char* op_result, const char* path, bool error_aborts) {
if ( op_result ) {
result = op_result;
error = false;
}
else {
if ( error_aborts )
reporter->InternalError("Path operation failed on %s: %s", path ? path : "<null>", strerror(errno));
else
error = true;
}
}
TEST_CASE("util flatten_script_name") { TEST_CASE("util flatten_script_name") {
CHECK(flatten_script_name("script", "some/path") == "some.path.script"); CHECK(flatten_script_name("script", "some/path") == "some.path.script");
#ifndef _MSC_VER #ifndef _MSC_VER
@ -1647,55 +1634,6 @@ FILE* open_file(const string& path, const string& mode) {
return rval; return rval;
} }
TEST_CASE("util path ops") {
#ifdef _MSC_VER
// TODO: adapt these tests to Windows paths
#else
SUBCASE("SafeDirname") {
SafeDirname d("/this/is/a/path", false);
CHECK(d.result == "/this/is/a");
SafeDirname d2("invalid", false);
CHECK(d2.result == ".");
SafeDirname d3("./filename", false);
CHECK(d2.result == ".");
}
SUBCASE("SafeBasename") {
SafeBasename b("/this/is/a/path", false);
CHECK(b.result == "path");
CHECK(! b.error);
SafeBasename b2("justafile", false);
CHECK(b2.result == "justafile");
CHECK(! b2.error);
}
#endif
}
SafeDirname::SafeDirname(const char* path, bool error_aborts) : SafePathOp() { DoFunc(path ? path : "", error_aborts); }
SafeDirname::SafeDirname(const string& path, bool error_aborts) : SafePathOp() { DoFunc(path, error_aborts); }
void SafeDirname::DoFunc(const string& path, bool error_aborts) {
char* tmp = copy_string(path.c_str());
CheckValid(dirname(tmp), tmp, error_aborts);
delete[] tmp;
}
SafeBasename::SafeBasename(const char* path, bool error_aborts) : SafePathOp() {
DoFunc(path ? path : "", error_aborts);
}
SafeBasename::SafeBasename(const string& path, bool error_aborts) : SafePathOp() { DoFunc(path, error_aborts); }
void SafeBasename::DoFunc(const string& path, bool error_aborts) {
char* tmp = copy_string(path.c_str());
CheckValid(basename(tmp), tmp, error_aborts);
delete[] tmp;
}
TEST_CASE("util implode_string_vector") { TEST_CASE("util implode_string_vector") {
std::vector<std::string> v = {"a", "b", "c"}; std::vector<std::string> v = {"a", "b", "c"};
CHECK(implode_string_vector(v, ",") == "a,b,c"); CHECK(implode_string_vector(v, ",") == "a,b,c");

View file

@ -92,19 +92,7 @@ namespace filesystem = ghc::filesystem;
inline constexpr std::string_view path_list_separator = ":"; inline constexpr std::string_view path_list_separator = ":";
#endif #endif
#include "zeek/3rdparty/nonstd/expected.hpp" #include "zeek/util-types.h"
namespace zeek {
template<typename T, typename E>
using expected = nonstd::expected<T, E>;
template<typename E>
using unexpected = nonstd::unexpected<E>;
} // namespace zeek
#include "zeek/Span.h"
using zeek_int_t = int64_t;
using zeek_uint_t = uint64_t;
#ifndef HAVE_STRCASESTR #ifndef HAVE_STRCASESTR
extern char* strcasestr(const char* s, const char* find); extern char* strcasestr(const char* s, const char* find);
@ -118,10 +106,6 @@ namespace zeek {
class ODesc; class ODesc;
class RecordVal; class RecordVal;
// Byte buffer types used by serialization code in storage and cluster.
using byte_buffer = std::vector<std::byte>;
using byte_buffer_span = Span<const std::byte>;
namespace util { namespace util {
namespace detail { namespace detail {
@ -220,21 +204,6 @@ bool is_package_loader(const std::string& path);
extern void add_to_zeek_path(const std::string& dir); extern void add_to_zeek_path(const std::string& dir);
/**
* Wrapper class for functions like dirname(3) or basename(3) that won't
* modify the path argument and may optionally abort execution on error.
*/
class SafePathOp {
public:
std::string result;
bool error;
protected:
SafePathOp() : result(), error() {}
void CheckValid(const char* result, const char* path, bool error_aborts);
};
/** /**
* Flatten a script name by replacing '/' path separators with '.'. * Flatten a script name by replacing '/' path separators with '.'.
* @param file A path to a Zeek script. If it is a __load__.zeek, that part * @param file A path to a Zeek script. If it is a __load__.zeek, that part
@ -398,24 +367,6 @@ extern const char* zeek_plugin_path();
extern const char* zeek_plugin_activate(); extern const char* zeek_plugin_activate();
extern std::string zeek_prefixes(); extern std::string zeek_prefixes();
class SafeDirname : public detail::SafePathOp {
public:
explicit SafeDirname(const char* path, bool error_aborts = true);
explicit SafeDirname(const std::string& path, bool error_aborts = true);
private:
void DoFunc(const std::string& path, bool error_aborts = true);
};
class SafeBasename : public detail::SafePathOp {
public:
explicit SafeBasename(const char* path, bool error_aborts = true);
explicit SafeBasename(const std::string& path, bool error_aborts = true);
private:
void DoFunc(const std::string& path, bool error_aborts = true);
};
std::string implode_string_vector(const std::vector<std::string>& v, const std::string& delim = "\n"); std::string implode_string_vector(const std::vector<std::string>& v, const std::string& delim = "\n");
/** /**