Skip to content

Commit

Permalink
Fix "NameError: uninitialized constant Datadog::Core::Vendor::IPAddr:…
Browse files Browse the repository at this point in the history
…:AddressFamilyError"

**What does this PR do?**:

This PR fixes a bug in the recently-introduced `IPAddr` module.

During review of #2665 the `IPAddr` module was introduced, and while
experimenting with the steep type checker, I noticed there was a
collision between our `IPAddr` module and the `IPAddr` module where
we were looking for execptions.

I'm not sure if this would happen in production, since we always
created an `IPAddr` instance with an IP, never a specific `family`,
but let's fix this anyway.

**Motivation**:

Less bugs!

**Additional Notes**:

(N/A)

**How to test the change?**:

This module is a backport of `IPAddr` to older Rubies. Previously,
we only tested it indirectly.

I added a minimal test to validate this change.
  • Loading branch information
ivoanjo committed Mar 7, 2023
1 parent 5dfea5e commit f24fd1e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/datadog/core/vendor/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def private?(ip)
when Socket::AF_INET6
addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000
else
raise IPAddr::AddressFamilyError, 'unsupported address family'
raise ::IPAddr::AddressFamilyError, 'unsupported address family'
end
end

Expand All @@ -55,7 +55,7 @@ def link_local?(ip)
when Socket::AF_INET6
addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000
else
raise IPAddr::AddressFamilyError, 'unsupported address family'
raise ::IPAddr::AddressFamilyError, 'unsupported address family'
end
end

Expand All @@ -68,7 +68,7 @@ def loopback?(ip)
when Socket::AF_INET6
addr == 1
else
raise IPAddr::AddressFamilyError, 'unsupported address family'
raise ::IPAddr::AddressFamilyError, 'unsupported address family'
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/datadog/core/vendor/ipaddr_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'datadog/core/vendor/ipaddr'
require 'ipaddr'
require 'ostruct'

RSpec.describe Datadog::Core::Vendor::IPAddr do
let(:invalid_object) { OpenStruct.new.tap { |o| o.instance_variable_set(:@addr, nil) } }

describe '.private?' do
it 'correctly raises when object is invalid' do
expect { described_class.private?(invalid_object) }.to raise_error(::IPAddr::AddressFamilyError)
end
end

describe '.link_local?' do
it 'correctly raises when object is invalid' do
expect { described_class.link_local?(invalid_object) }.to raise_error(::IPAddr::AddressFamilyError)
end
end

describe '.loopback?' do
it 'correctly raises when object is invalid' do
expect { described_class.loopback?(invalid_object) }.to raise_error(::IPAddr::AddressFamilyError)
end
end
end

0 comments on commit f24fd1e

Please sign in to comment.