From 7a5209855fef71d7b4e540d486be9a19bb710c11 Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Thu, 24 Jul 2025 18:29:08 -0700 Subject: [PATCH] Add ODesc::Size() that returns size_t, deprecate ODesc::Len() --- src/Desc.cc | 15 ++++++++------- src/Desc.h | 18 +++++++++++------- src/Expr.cc | 2 +- src/OpaqueVal.cc | 2 +- src/Stmt.cc | 2 +- src/Val.cc | 6 +++--- src/logging/writers/ascii/Ascii.cc | 7 ++----- src/logging/writers/sqlite/SQLite.cc | 4 ++-- src/script_opt/ZAM/BuiltInSupport.h | 4 ++-- src/script_opt/ZAM/Support.cc | 2 +- src/strings.bif | 4 ++-- src/zeek.bif | 26 +++++++++++++------------- 12 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/Desc.cc b/src/Desc.cc index d11c9f4ab6..c49f2d7139 100644 --- a/src/Desc.cc +++ b/src/Desc.cc @@ -11,8 +11,8 @@ #include "zeek/IPAddr.h" #include "zeek/Reporter.h" -constexpr unsigned int DEFAULT_SIZE = 128; -constexpr int SLOP = 10; +constexpr size_t DEFAULT_SIZE = 128; +constexpr size_t SLOP = 10; namespace zeek { @@ -74,7 +74,7 @@ void ODesc::PopIndentNoNL() { } void ODesc::Add(const char* s, int do_indent) { - unsigned int n = strlen(s); + size_t n = strlen(s); if ( do_indent && IsReadable() && offset > 0 && ((const char*)base)[offset - 1] == '\n' ) Indent(); @@ -235,7 +235,7 @@ std::pair ODesc::FirstEscapeLoc(const char* bytes, size_t n return {nullptr, 0}; } -void ODesc::AddBytes(const void* bytes, unsigned int n) { +void ODesc::AddBytes(const void* bytes, size_t n) { if ( ! escape ) { AddBytesRaw(bytes, n); return; @@ -271,7 +271,7 @@ void ODesc::AddBytes(const void* bytes, unsigned int n) { } } -void ODesc::AddBytesRaw(const void* bytes, unsigned int n) { +void ODesc::AddBytesRaw(const void* bytes, size_t n) { if ( n == 0 ) return; @@ -304,7 +304,7 @@ void ODesc::AddBytesRaw(const void* bytes, unsigned int n) { } } -void ODesc::Grow(unsigned int n) { +void ODesc::Grow(size_t n) { bool size_changed = false; while ( offset + n + SLOP >= size ) { size *= 2; @@ -319,7 +319,8 @@ void ODesc::Clear() { offset = 0; // If we've allocated an exceedingly large amount of space, free it. - if ( size > 10 * 1024 * 1024 ) { + constexpr size_t too_large = 10l * 1024 * 1024; + if ( size > too_large ) { free(base); size = DEFAULT_SIZE; base = util::safe_malloc(size); diff --git a/src/Desc.h b/src/Desc.h index c2aa0bab3e..25bcddf0cf 100644 --- a/src/Desc.h +++ b/src/Desc.h @@ -144,7 +144,11 @@ public: return byte_vec(t); } - int Len() const { return offset; } + [[deprecated("Remove in v8.1. Use Size() that returns size_t instead.")]] + int Len() const { + return static_cast(offset); + } + size_t Size() const { return offset; } void Clear(); @@ -157,11 +161,11 @@ public: protected: void Indent(); - void AddBytes(const void* bytes, unsigned int n); - void AddBytesRaw(const void* bytes, unsigned int n); + void AddBytes(const void* bytes, size_t n); + void AddBytesRaw(const void* bytes, size_t n); // Make buffer big enough for n bytes beyond bufp. - void Grow(unsigned int n); + void Grow(size_t n); /** * Returns the location of the first place in the bytes to be hex-escaped. @@ -188,9 +192,9 @@ protected: DescType type; DescStyle style; - void* base; // beginning of buffer - unsigned int offset; // where we are in the buffer - unsigned int size; // size of buffer in bytes + void* base; // beginning of buffer + size_t offset; // where we are in the buffer + size_t size; // size of buffer in bytes bool utf8; // whether valid utf-8 sequences may pass through unescaped bool escape; // escape unprintable characters in output? diff --git a/src/Expr.cc b/src/Expr.cc index 96c6a0d46d..a0e076443a 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -4369,7 +4369,7 @@ void LambdaExpr::BuildName() { for ( ;; ) { hash128_t h; - KeyedHash::Hash128(d.Bytes(), d.Len(), &h); + KeyedHash::Hash128(d.Bytes(), d.Size(), &h); my_name = "lambda_<" + std::to_string(h[0]) + ">"; auto fullname = make_full_var_name(current_module.data(), my_name.data()); diff --git a/src/OpaqueVal.cc b/src/OpaqueVal.cc index 553c52d654..adc309fc4a 100644 --- a/src/OpaqueVal.cc +++ b/src/OpaqueVal.cc @@ -401,7 +401,7 @@ void HashVal::digest_one(detail::HashDigestState* h, const Val* v) { else { ODesc d(DESC_BINARY); v->Describe(&d); - detail::hash_update(h, (const u_char*)d.Bytes(), d.Len()); + detail::hash_update(h, (const u_char*)d.Bytes(), d.Size()); } } diff --git a/src/Stmt.cc b/src/Stmt.cc index 78789a6e24..88552848fb 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -345,7 +345,7 @@ void do_print_stmt(const std::vector& vals) { d.SetStyle(style); describe_vals(vals, &d, offset); - f->Write(d.Description(), d.Len()); + f->Write(d.Description(), d.Size()); } else { ODesc d(DESC_READABLE, f); diff --git a/src/Val.cc b/src/Val.cc index 13ff13c67e..a064b8aab1 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -397,7 +397,7 @@ static void BuildJSON(json::detail::NullDoubleWriter& writer, Val* val, bool onl ODesc d; d.SetStyle(RAW_STYLE); val->Describe(&d); - writer.String(reinterpret_cast(d.Bytes()), d.Len()); + writer.String(reinterpret_cast(d.Bytes()), d.Size()); break; } @@ -408,7 +408,7 @@ static void BuildJSON(json::detail::NullDoubleWriter& writer, Val* val, bool onl ODesc d; d.SetStyle(RAW_STYLE); val->Describe(&d); - writer.String(reinterpret_cast(d.Bytes()), d.Len()); + writer.String(reinterpret_cast(d.Bytes()), d.Size()); } break; } @@ -420,7 +420,7 @@ static void BuildJSON(json::detail::NullDoubleWriter& writer, Val* val, bool onl ODesc d; d.SetStyle(RAW_STYLE); val->Describe(&d); - std::string desc(reinterpret_cast(d.Bytes()), d.Len()); + std::string desc(reinterpret_cast(d.Bytes()), d.Size()); // None of our function types should have surrounding // whitespace, but ODesc might produce it due to its diff --git a/src/logging/writers/ascii/Ascii.cc b/src/logging/writers/ascii/Ascii.cc index b5910e1eac..f012871309 100644 --- a/src/logging/writers/ascii/Ascii.cc +++ b/src/logging/writers/ascii/Ascii.cc @@ -240,10 +240,7 @@ void Ascii::InitConfigOptions() { meta_prefix.assign((const char*)BifConst::LogAscii::meta_prefix->Bytes(), BifConst::LogAscii::meta_prefix->Len()); - ODesc tsfmt; - BifConst::LogAscii::json_timestamps->Describe(&tsfmt); - json_timestamps.assign((const char*)tsfmt.Bytes(), tsfmt.Len()); - + json_timestamps = zeek::obj_desc_short(BifConst::LogAscii::json_timestamps); json_include_unset_fields = BifConst::LogAscii::json_include_unset_fields; gzip_file_extension.assign((const char*)BifConst::LogAscii::gzip_file_extension->Bytes(), @@ -591,7 +588,7 @@ bool Ascii::DoWrite(int num_fields, const threading::Field* const* fields, threa desc.AddRaw("\n", 1); const char* bytes = (const char*)desc.Bytes(); - int len = desc.Len(); + size_t len = desc.Size(); if ( strncmp(bytes, meta_prefix.data(), meta_prefix.size()) == 0 ) { // It would so escape the first character. diff --git a/src/logging/writers/sqlite/SQLite.cc b/src/logging/writers/sqlite/SQLite.cc index 8a64c71e86..fa4f3886ba 100644 --- a/src/logging/writers/sqlite/SQLite.cc +++ b/src/logging/writers/sqlite/SQLite.cc @@ -318,7 +318,7 @@ int SQLite::AddParams(Value* val, int pos) { } desc.RemoveEscapeSequence(set_separator); - return sqlite3_bind_text(st, pos, (const char*)desc.Bytes(), desc.Len(), SQLITE_TRANSIENT); + return sqlite3_bind_text(st, pos, (const char*)desc.Bytes(), desc.Size(), SQLITE_TRANSIENT); } case TYPE_VECTOR: { @@ -338,7 +338,7 @@ int SQLite::AddParams(Value* val, int pos) { } desc.RemoveEscapeSequence(set_separator); - return sqlite3_bind_text(st, pos, (const char*)desc.Bytes(), desc.Len(), SQLITE_TRANSIENT); + return sqlite3_bind_text(st, pos, (const char*)desc.Bytes(), desc.Size(), SQLITE_TRANSIENT); } default: Error(Fmt("unsupported field format %d", val->type)); return 0; diff --git a/src/script_opt/ZAM/BuiltInSupport.h b/src/script_opt/ZAM/BuiltInSupport.h index 340db9f8ab..aafe308533 100644 --- a/src/script_opt/ZAM/BuiltInSupport.h +++ b/src/script_opt/ZAM/BuiltInSupport.h @@ -88,7 +88,7 @@ public: DescCatArg(TypePtr _t) : CatArg(), t(std::move(_t)) { d.SetStyle(RAW_STYLE); } void RenderInto(const ZVal& zv, char*& res) override { - auto n = d.Len(); + auto n = d.Size(); memcpy(res, d.Bytes(), n); res += n; d.Clear(); @@ -97,7 +97,7 @@ public: protected: size_t ComputeMaxSize(const ZVal& zv) override { zv.ToVal(t)->Describe(&d); - return d.Len(); + return d.Size(); } ODesc d; diff --git a/src/script_opt/ZAM/Support.cc b/src/script_opt/ZAM/Support.cc index add07f095b..3c606335cb 100644 --- a/src/script_opt/ZAM/Support.cc +++ b/src/script_opt/ZAM/Support.cc @@ -197,7 +197,7 @@ StringValPtr ZAM_val_cat(const ValPtr& v) { v->Describe(&d); - String* s = new String(true, d.TakeBytes(), d.Len()); + String* s = new String(true, d.TakeBytes(), d.Size()); s->SetUseFreeToDelete(true); return make_intrusive(s); diff --git a/src/strings.bif b/src/strings.bif index 6dc98f5772..d9728bb938 100644 --- a/src/strings.bif +++ b/src/strings.bif @@ -121,7 +121,7 @@ function join_string_vec%(vec: string_vec, sep: string%): string e->Describe(&d); } - zeek::String* s = new zeek::String(true, d.TakeBytes(), d.Len()); + zeek::String* s = new zeek::String(true, d.TakeBytes(), d.Size()); s->SetUseFreeToDelete(true); return zeek::make_intrusive(s); @@ -179,7 +179,7 @@ function join_string_set%(ss: string_set, sep: string%): string ++i; } - zeek::String* str = new zeek::String(true, d.TakeBytes(), d.Len()); + zeek::String* str = new zeek::String(true, d.TakeBytes(), d.Size()); str->SetUseFreeToDelete(true); return zeek::make_intrusive(str); diff --git a/src/zeek.bif b/src/zeek.bif index 6fd4bba6e0..210aa12a69 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -266,13 +266,13 @@ static void do_fmt(const char*& fmt, zeek::Val* v, zeek::ODesc* d) d->Add(" "); } - d->AddN((const char*)(s.Bytes()), s.Len()); + d->AddN((const char*)(s.Bytes()), s.Size()); // Right-padding with whitespace, if any. if ( field_width > 0 && left_just ) { - int sl = s.Len(); - while ( ++sl <= field_width ) + size_t sl = s.Size(); + while ( ++sl <= static_cast(field_width) ) d->Add(" "); } @@ -1052,23 +1052,23 @@ template IntType fnva(zeek::Val* input, IntType offset, IntType prime) { zeek::ODesc desc(zeek::DESC_BINARY); - auto length = 0; + size_t length = 0; const u_char* bytes = nullptr; if (input->GetType()->Tag() == zeek::TYPE_STRING) { - length = ((zeek::StringVal*) input) -> Len(); - bytes = ((zeek::StringVal*) input)-> Bytes(); + length = ((zeek::StringVal*) input)->Len(); + bytes = ((zeek::StringVal*) input)->Bytes(); } else { input->Describe(&desc); bytes = desc.Bytes(); - length = desc.Len(); + length = desc.Size(); } IntType rval = offset; - for ( auto i = 0; i < length; ++i ) + for ( size_t i = 0; i < length; ++i ) { rval ^= bytes[i]; rval *= prime; @@ -1747,7 +1747,7 @@ function cat%(...%): string for ( const auto& a : @ARG@ ) a->Describe(&d); - String* s = new String(true, d.TakeBytes(), d.Len()); + String* s = new String(true, d.TakeBytes(), d.Size()); s->SetUseFreeToDelete(true); return zeek::make_intrusive(s); @@ -1784,7 +1784,7 @@ function cat_sep%(sep: string, def: string, ...%): string v->Describe(&d); } - String* s = new String(true, d.TakeBytes(), d.Len()); + String* s = new String(true, d.TakeBytes(), d.Size()); s->SetUseFreeToDelete(true); return zeek::make_intrusive(s); @@ -1859,7 +1859,7 @@ function fmt%(...%): string return zeek::val_mgr->EmptyString(); } - String* s = new String(true, d.TakeBytes(), d.Len()); + String* s = new String(true, d.TakeBytes(), d.Size()); s->SetUseFreeToDelete(true); return zeek::make_intrusive(s); @@ -1877,7 +1877,7 @@ function print_raw%(...%): bool zeek::ODesc d(DESC_READABLE); d.SetStyle(RAW_STYLE); describe_vals(@ARG@, &d, 0); - printf("%.*s", d.Len(), d.Description()); + printf("%.*s", static_cast(d.Size()), d.Description()); return zeek::val_mgr->Bool(true); %} @@ -2074,7 +2074,7 @@ function type_name%(t: any%): string zeek::ODesc d; t->GetType()->Describe(&d); - String* s = new String(true, d.TakeBytes(), d.Len()); + String* s = new String(true, d.TakeBytes(), d.Size()); s->SetUseFreeToDelete(true); return zeek::make_intrusive(s);