random() rejects the oui option its own example uses, and ignores die_on_error
- Dominant language
- Perl
- Stars
- 4
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Description
`random()` reads only the `prefix` option. Its internal comment says `oui`, and `examples/example.pl` calls it with `oui =>`, so the shipped example dies. Separately, `random()` decides whether to croak from an undocumented `_die` option rather than the documented `die_on_error`, so callers cannot get exceptions from it the way they can from `new`.
**Reproduce**
```
$ perl -Ilib examples/example.pl 2>&1 | tail -1
Can't call method "as_ieee" on an undefined value at examples/example.pl line 58.
$ perl -Ilib -e 'use NetAddr::MAC;
print "oui option: ", (NetAddr::MAC->random(oui => "00:16:3e") // "undef: $NetAddr::MAC::errstr"), "\n";
my $r = eval { NetAddr::MAC->random(prefix => "zz", die_on_error => 1) }; print "die_on_error: ", ($@ ? "croaked" : "did not croak"), "\n";
$r = eval { NetAddr::MAC->random(prefix => "zz", _die => 1) }; print "_die: ", ($@ ? "croaked" : "did not croak"), "\n"'
oui option: undef: Please provide an oui prefix
die_on_error: did not croak
_die: croaked
```
**Cause**
`lib/NetAddr/MAC.pm:449-451` (`$args{prefix}` only) and lines 455 and 467 (`$args{_die}`).
The fix accepts `oui` as an alias for `prefix`, computes the effective die flag from `die_on_error` exactly as `_init` does, passes it through to `_init`, documents the alias, and changes the example to the primary name.
**Verification**
After the diff `examples/example.pl` runs to completion and prints both random addresses; `random(oui => ...)` returns an object; `die_on_error => 1` croaks with `Prefix must be between 3 and 5 octets for EUI-48`; `_die` is ignored. `t/133` passes. Full suite passes.
Applies on top of #15.
**Suggested fix**
```diff
diff --git a/lib/NetAddr/MAC.pm b/lib/NetAddr/MAC.pm
index 4f2ff6d..ab1aa69 100644
--- a/lib/NetAddr/MAC.pm
+++ b/lib/NetAddr/MAC.pm
@@ -409,6 +409,7 @@ Generates a random MAC address using the provided OUI/prefix.
my $mac = NetAddr::MAC->random( prefix => '00:16:3e:12', eui64 => 1 );
The prefix can be any string format accepted by the module (e.g., colon, dash, dot, or plain hex).
+C is accepted as an alias for C. C behaves as it does for B.
You must provide at least 3 octets for EUI-48 (default) or at least 4 for EUI-64 (with C 1>).
You may provide more than the minimum; any missing octets will be filled with random values up to 6 (EUI-48) or 8 (EUI-64) total.
@@ -436,15 +437,17 @@ sub random {
# clear the errstr, see also RT96045
$NetAddr::MAC::errstr = undef;
- # Accept options: oui => ..., eui64 => 1/0
+ # Accept options: prefix => ... (oui => ... is an alias), eui64 => 1/0,
+ # die_on_error => 1/0
my %args = @q % 2 ? ( prefix => shift @q, @q ) : @q;
- my $oui_str = $args{prefix};
+ my $oui_str = $args{prefix} // $args{oui};
+ my $die = defined $args{die_on_error}
+ ? ( $args{die_on_error} ? 1 : 0 )
+ : ( $NetAddr::MAC::die_on_error ? 1 : 0 );
unless ($oui_str) {
my $e = q|Please provide an oui prefix|;
- if ($NetAddr::MAC::die_on_error or $args{_die}) {
- croak "$e\n";
- }
+ croak "$e\n" if $die;
$NetAddr::MAC::errstr = $e;
return
}
@@ -454,9 +457,7 @@ sub random {
my $oui_ints = _oui_to_integers($oui_str, $min, $max);
unless ($oui_ints) {
my $e = "Prefix must be between $min and $max octets for ".($eui64 ? 'EUI-64' : 'EUI-48');
- if ($NetAddr::MAC::die_on_error or $args{_die}) {
- croak "$e\n";
- }
+ croak "$e\n" if $die;
$NetAddr::MAC::errstr = $e;
return
}
@@ -468,7 +469,7 @@ sub random {
my $c = ref($p) || $p;
my $self = bless {}, $c;
- $self->_init( mac => $mac_str )
+ $self->_init( mac => $mac_str, die_on_error => $die )
or return;
return $self
diff --git a/examples/example.pl b/examples/example.pl
index a4e4799..1c84886 100644
--- a/examples/example.pl
+++ b/examples/example.pl
@@ -54,7 +54,7 @@ if (!$bad) {
}
# Random MAC generation
-my $rand_mac = NetAddr::MAC->random(oui => '00:16:3e');
+my $rand_mac = NetAddr::MAC->random(prefix => '00:16:3e');
print "\nRandom MAC (EUI-48, OUI 00:16:3e): ", $rand_mac->as_ieee, "\n";
-my $rand_mac64 = NetAddr::MAC->random(oui => '00:16:3e:12', eui64 => 1);
+my $rand_mac64 = NetAddr::MAC->random(prefix => '00:16:3e:12', eui64 => 1);
print "Random MAC (EUI-64, OUI 00:16:3e:12): ", $rand_mac64->as_ieee, "\n";
```
Found during a review of master at 4a255fa (v1.01).
Contributor guide
Research direction
Start with random() in lib/NetAddr/MAC.pm around lines 449-467 and compare its option handling with _init; review the random() documentation and examples/example.pl. Run examples/example.pl and t/133 first, then verify that oui and prefix both work, die_on_error raises the documented error, and _die is ignored; the full suite should pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- perl
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 92/100