Add ODesc::Size() that returns size_t, deprecate ODesc::Len()

This commit is contained in:
Tim Wojtulewicz 2025-07-24 18:29:08 -07:00
parent 6e2a18ce4f
commit 7a5209855f
12 changed files with 47 additions and 45 deletions

View file

@ -11,8 +11,8 @@
#include "zeek/IPAddr.h" #include "zeek/IPAddr.h"
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
constexpr unsigned int DEFAULT_SIZE = 128; constexpr size_t DEFAULT_SIZE = 128;
constexpr int SLOP = 10; constexpr size_t SLOP = 10;
namespace zeek { namespace zeek {
@ -74,7 +74,7 @@ void ODesc::PopIndentNoNL() {
} }
void ODesc::Add(const char* s, int do_indent) { 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' ) if ( do_indent && IsReadable() && offset > 0 && ((const char*)base)[offset - 1] == '\n' )
Indent(); Indent();
@ -235,7 +235,7 @@ std::pair<const char*, size_t> ODesc::FirstEscapeLoc(const char* bytes, size_t n
return {nullptr, 0}; return {nullptr, 0};
} }
void ODesc::AddBytes(const void* bytes, unsigned int n) { void ODesc::AddBytes(const void* bytes, size_t n) {
if ( ! escape ) { if ( ! escape ) {
AddBytesRaw(bytes, n); AddBytesRaw(bytes, n);
return; 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 ) if ( n == 0 )
return; 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; bool size_changed = false;
while ( offset + n + SLOP >= size ) { while ( offset + n + SLOP >= size ) {
size *= 2; size *= 2;
@ -319,7 +319,8 @@ void ODesc::Clear() {
offset = 0; offset = 0;
// If we've allocated an exceedingly large amount of space, free it. // 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); free(base);
size = DEFAULT_SIZE; size = DEFAULT_SIZE;
base = util::safe_malloc(size); base = util::safe_malloc(size);

View file

@ -144,7 +144,11 @@ public:
return byte_vec(t); 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<int>(offset);
}
size_t Size() const { return offset; }
void Clear(); void Clear();
@ -157,11 +161,11 @@ public:
protected: protected:
void Indent(); void Indent();
void AddBytes(const void* bytes, unsigned int n); void AddBytes(const void* bytes, size_t n);
void AddBytesRaw(const void* bytes, unsigned int n); void AddBytesRaw(const void* bytes, size_t n);
// Make buffer big enough for n bytes beyond bufp. // 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. * Returns the location of the first place in the bytes to be hex-escaped.
@ -188,9 +192,9 @@ protected:
DescType type; DescType type;
DescStyle style; DescStyle style;
void* base; // beginning of buffer void* base; // beginning of buffer
unsigned int offset; // where we are in the buffer size_t offset; // where we are in the buffer
unsigned int size; // size of buffer in bytes size_t size; // size of buffer in bytes
bool utf8; // whether valid utf-8 sequences may pass through unescaped bool utf8; // whether valid utf-8 sequences may pass through unescaped
bool escape; // escape unprintable characters in output? bool escape; // escape unprintable characters in output?

View file

@ -4369,7 +4369,7 @@ void LambdaExpr::BuildName() {
for ( ;; ) { for ( ;; ) {
hash128_t h; 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]) + ">"; my_name = "lambda_<" + std::to_string(h[0]) + ">";
auto fullname = make_full_var_name(current_module.data(), my_name.data()); auto fullname = make_full_var_name(current_module.data(), my_name.data());

View file

@ -401,7 +401,7 @@ void HashVal::digest_one(detail::HashDigestState* h, const Val* v) {
else { else {
ODesc d(DESC_BINARY); ODesc d(DESC_BINARY);
v->Describe(&d); 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());
} }
} }

View file

@ -345,7 +345,7 @@ void do_print_stmt(const std::vector<ValPtr>& vals) {
d.SetStyle(style); d.SetStyle(style);
describe_vals(vals, &d, offset); describe_vals(vals, &d, offset);
f->Write(d.Description(), d.Len()); f->Write(d.Description(), d.Size());
} }
else { else {
ODesc d(DESC_READABLE, f); ODesc d(DESC_READABLE, f);

View file

@ -397,7 +397,7 @@ static void BuildJSON(json::detail::NullDoubleWriter& writer, Val* val, bool onl
ODesc d; ODesc d;
d.SetStyle(RAW_STYLE); d.SetStyle(RAW_STYLE);
val->Describe(&d); val->Describe(&d);
writer.String(reinterpret_cast<const char*>(d.Bytes()), d.Len()); writer.String(reinterpret_cast<const char*>(d.Bytes()), d.Size());
break; break;
} }
@ -408,7 +408,7 @@ static void BuildJSON(json::detail::NullDoubleWriter& writer, Val* val, bool onl
ODesc d; ODesc d;
d.SetStyle(RAW_STYLE); d.SetStyle(RAW_STYLE);
val->Describe(&d); val->Describe(&d);
writer.String(reinterpret_cast<const char*>(d.Bytes()), d.Len()); writer.String(reinterpret_cast<const char*>(d.Bytes()), d.Size());
} }
break; break;
} }
@ -420,7 +420,7 @@ static void BuildJSON(json::detail::NullDoubleWriter& writer, Val* val, bool onl
ODesc d; ODesc d;
d.SetStyle(RAW_STYLE); d.SetStyle(RAW_STYLE);
val->Describe(&d); val->Describe(&d);
std::string desc(reinterpret_cast<const char*>(d.Bytes()), d.Len()); std::string desc(reinterpret_cast<const char*>(d.Bytes()), d.Size());
// None of our function types should have surrounding // None of our function types should have surrounding
// whitespace, but ODesc might produce it due to its // whitespace, but ODesc might produce it due to its

View file

@ -240,10 +240,7 @@ void Ascii::InitConfigOptions() {
meta_prefix.assign((const char*)BifConst::LogAscii::meta_prefix->Bytes(), BifConst::LogAscii::meta_prefix->Len()); meta_prefix.assign((const char*)BifConst::LogAscii::meta_prefix->Bytes(), BifConst::LogAscii::meta_prefix->Len());
ODesc tsfmt; json_timestamps = zeek::obj_desc_short(BifConst::LogAscii::json_timestamps);
BifConst::LogAscii::json_timestamps->Describe(&tsfmt);
json_timestamps.assign((const char*)tsfmt.Bytes(), tsfmt.Len());
json_include_unset_fields = BifConst::LogAscii::json_include_unset_fields; json_include_unset_fields = BifConst::LogAscii::json_include_unset_fields;
gzip_file_extension.assign((const char*)BifConst::LogAscii::gzip_file_extension->Bytes(), 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); desc.AddRaw("\n", 1);
const char* bytes = (const char*)desc.Bytes(); 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 ) { if ( strncmp(bytes, meta_prefix.data(), meta_prefix.size()) == 0 ) {
// It would so escape the first character. // It would so escape the first character.

View file

@ -318,7 +318,7 @@ int SQLite::AddParams(Value* val, int pos) {
} }
desc.RemoveEscapeSequence(set_separator); 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: { case TYPE_VECTOR: {
@ -338,7 +338,7 @@ int SQLite::AddParams(Value* val, int pos) {
} }
desc.RemoveEscapeSequence(set_separator); 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; default: Error(Fmt("unsupported field format %d", val->type)); return 0;

View file

@ -88,7 +88,7 @@ public:
DescCatArg(TypePtr _t) : CatArg(), t(std::move(_t)) { d.SetStyle(RAW_STYLE); } DescCatArg(TypePtr _t) : CatArg(), t(std::move(_t)) { d.SetStyle(RAW_STYLE); }
void RenderInto(const ZVal& zv, char*& res) override { void RenderInto(const ZVal& zv, char*& res) override {
auto n = d.Len(); auto n = d.Size();
memcpy(res, d.Bytes(), n); memcpy(res, d.Bytes(), n);
res += n; res += n;
d.Clear(); d.Clear();
@ -97,7 +97,7 @@ public:
protected: protected:
size_t ComputeMaxSize(const ZVal& zv) override { size_t ComputeMaxSize(const ZVal& zv) override {
zv.ToVal(t)->Describe(&d); zv.ToVal(t)->Describe(&d);
return d.Len(); return d.Size();
} }
ODesc d; ODesc d;

View file

@ -197,7 +197,7 @@ StringValPtr ZAM_val_cat(const ValPtr& v) {
v->Describe(&d); v->Describe(&d);
String* s = new String(true, d.TakeBytes(), d.Len()); String* s = new String(true, d.TakeBytes(), d.Size());
s->SetUseFreeToDelete(true); s->SetUseFreeToDelete(true);
return make_intrusive<StringVal>(s); return make_intrusive<StringVal>(s);

View file

@ -121,7 +121,7 @@ function join_string_vec%(vec: string_vec, sep: string%): string
e->Describe(&d); 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); s->SetUseFreeToDelete(true);
return zeek::make_intrusive<zeek::StringVal>(s); return zeek::make_intrusive<zeek::StringVal>(s);
@ -179,7 +179,7 @@ function join_string_set%(ss: string_set, sep: string%): string
++i; ++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); str->SetUseFreeToDelete(true);
return zeek::make_intrusive<zeek::StringVal>(str); return zeek::make_intrusive<zeek::StringVal>(str);

View file

@ -266,13 +266,13 @@ static void do_fmt(const char*& fmt, zeek::Val* v, zeek::ODesc* d)
d->Add(" "); d->Add(" ");
} }
d->AddN((const char*)(s.Bytes()), s.Len()); d->AddN((const char*)(s.Bytes()), s.Size());
// Right-padding with whitespace, if any. // Right-padding with whitespace, if any.
if ( field_width > 0 && left_just ) if ( field_width > 0 && left_just )
{ {
int sl = s.Len(); size_t sl = s.Size();
while ( ++sl <= field_width ) while ( ++sl <= static_cast<size_t>(field_width) )
d->Add(" "); d->Add(" ");
} }
@ -1052,23 +1052,23 @@ template<typename IntType>
IntType fnva(zeek::Val* input, IntType offset, IntType prime) { IntType fnva(zeek::Val* input, IntType offset, IntType prime) {
zeek::ODesc desc(zeek::DESC_BINARY); zeek::ODesc desc(zeek::DESC_BINARY);
auto length = 0; size_t length = 0;
const u_char* bytes = nullptr; const u_char* bytes = nullptr;
if (input->GetType()->Tag() == zeek::TYPE_STRING) if (input->GetType()->Tag() == zeek::TYPE_STRING)
{ {
length = ((zeek::StringVal*) input) -> Len(); length = ((zeek::StringVal*) input)->Len();
bytes = ((zeek::StringVal*) input)-> Bytes(); bytes = ((zeek::StringVal*) input)->Bytes();
} }
else else
{ {
input->Describe(&desc); input->Describe(&desc);
bytes = desc.Bytes(); bytes = desc.Bytes();
length = desc.Len(); length = desc.Size();
} }
IntType rval = offset; IntType rval = offset;
for ( auto i = 0; i < length; ++i ) for ( size_t i = 0; i < length; ++i )
{ {
rval ^= bytes[i]; rval ^= bytes[i];
rval *= prime; rval *= prime;
@ -1747,7 +1747,7 @@ function cat%(...%): string
for ( const auto& a : @ARG@ ) for ( const auto& a : @ARG@ )
a->Describe(&d); a->Describe(&d);
String* s = new String(true, d.TakeBytes(), d.Len()); String* s = new String(true, d.TakeBytes(), d.Size());
s->SetUseFreeToDelete(true); s->SetUseFreeToDelete(true);
return zeek::make_intrusive<zeek::StringVal>(s); return zeek::make_intrusive<zeek::StringVal>(s);
@ -1784,7 +1784,7 @@ function cat_sep%(sep: string, def: string, ...%): string
v->Describe(&d); v->Describe(&d);
} }
String* s = new String(true, d.TakeBytes(), d.Len()); String* s = new String(true, d.TakeBytes(), d.Size());
s->SetUseFreeToDelete(true); s->SetUseFreeToDelete(true);
return zeek::make_intrusive<zeek::StringVal>(s); return zeek::make_intrusive<zeek::StringVal>(s);
@ -1859,7 +1859,7 @@ function fmt%(...%): string
return zeek::val_mgr->EmptyString(); 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); s->SetUseFreeToDelete(true);
return zeek::make_intrusive<zeek::StringVal>(s); return zeek::make_intrusive<zeek::StringVal>(s);
@ -1877,7 +1877,7 @@ function print_raw%(...%): bool
zeek::ODesc d(DESC_READABLE); zeek::ODesc d(DESC_READABLE);
d.SetStyle(RAW_STYLE); d.SetStyle(RAW_STYLE);
describe_vals(@ARG@, &d, 0); describe_vals(@ARG@, &d, 0);
printf("%.*s", d.Len(), d.Description()); printf("%.*s", static_cast<int>(d.Size()), d.Description());
return zeek::val_mgr->Bool(true); return zeek::val_mgr->Bool(true);
%} %}
@ -2074,7 +2074,7 @@ function type_name%(t: any%): string
zeek::ODesc d; zeek::ODesc d;
t->GetType()->Describe(&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); s->SetUseFreeToDelete(true);
return zeek::make_intrusive<zeek::StringVal>(s); return zeek::make_intrusive<zeek::StringVal>(s);