Skip to content

Conversation

mozillazg
Copy link
Contributor

The old code will cause parsed IPv4 without version, srcIP, dstIP, and more fields, but only have options.

@mosajjal mosajjal self-assigned this Jul 14, 2025
@mosajjal mosajjal requested a review from Copilot July 14, 2025 22:20
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes premature termination in IPv4 option parsing to ensure full header fields (Checksum, SrcIP, DstIP) are populated, and adds a new test case for multiple option types.

  • Removed the early return in the options loop so subsequent header fields are set after options.
  • Preserved parsed padding by removing ip.Padding = nil at the end of DecodeFromBytes.
  • Added a subtest in TestIPv4Options for packets with multiple options and wrapped tests in t.Run.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
layers/ip4.go Removed premature return in options parsing, adjusted loop label and padding handling
layers/ip4_test.go Added a new multi-option test case, introduced t.Run, and moved ip4 initialization into subtests
Comments suppressed due to low confidence (1)

layers/ip4_test.go:195

  • [nitpick] The comment suggests reusing ip4 across iterations, but ip4 is now reinitialized inside each subtest. Consider updating or removing this comment to reflect the current initialization strategy.
			var ip4 IPv4 // reuse ip4 to test reset


return nil
headerOptionsData = headerOptionsData[1:]
break pullOutOptions
case 1: // 1 byte padding
Copy link
Preview

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

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

The code for option type 1 (NOP) does not append the parsed option to ip.Options, so OptionType 1 entries are omitted. Consider adding ip.Options = append(ip.Options, opt) before advancing headerOptionsData.

Copilot uses AI. Check for mistakes.

if !bytes.Equal(ip4.Padding, test.padding) {
t.Fatalf("Padding mismatch.\nGot:\n%#v\nExpected:\n%#v\n", ip4.Padding, test.padding)
}
t.Run(test.packet, func(t *testing.T) {
Copy link
Preview

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

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

[nitpick] The table-driven test uses the range variable test inside t.Run closures, which can lead to unexpected captures if tests run in parallel. Consider shadowing the loop variable (e.g., test := test) before invoking t.Run to bind the current value.

Copilot uses AI. Check for mistakes.

@mosajjal mosajjal merged commit be3fffa into gopacket:master Jul 21, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants