Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Log::Metadata#dup crash with 2+ entries #13369

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spec/std/log/metadata_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ describe Log::Metadata do
m({a: 1}).extend({} of Symbol => String).should_not be_empty
end

describe "#dup" do
it "creates a shallow copy" do
Log::Metadata.empty.dup.should eq(Log::Metadata.empty)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use:

Suggested change
Log::Metadata.empty.dup.should eq(Log::Metadata.empty)
Log::Metadata.empty.dup.should be(Log::Metadata.empty)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be testing .empty more than #dup... But yeah technically, all expectations could use be. But I think that's actually an implementation detail. It's not strictly necessary for #dup to return the same instance, but it does that for performance reasons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't part of #dup's contract to preserve object identity of immutable reference types, one day #dup might create a shallow copy for other reasons. So eq suffices for unit tests of #dup

m({a: 1}).dup.should eq(m({a: 1}))
m({a: 1, b: 3}).dup.should eq(m({a: 1, b: 3}))
end
end

it "extend" do
m({a: 1}).extend({b: 2}).should eq(m({a: 1, b: 2}))
m({a: 1, b: 3}).extend({b: 2}).should eq(m({a: 1, b: 2}))
Expand Down
4 changes: 4 additions & 0 deletions src/log/metadata.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class Log::Metadata
data
end

def dup : self
self
end

protected def setup(@parent : Metadata?, entries : NamedTuple | Hash)
@size = @overridden_size = entries.size
@max_total_size = @size + (@parent.try(&.max_total_size) || 0)
Expand Down