Skip to content

Fix decoding inet with netmask#19

Merged
tsloughter merged 1 commit intotsloughter:mainfrom
leah-u:fix-inet-decoding
Dec 8, 2025
Merged

Fix decoding inet with netmask#19
tsloughter merged 1 commit intotsloughter:mainfrom
leah-u:fix-inet-decoding

Conversation

@leah-u
Copy link
Copy Markdown
Contributor

@leah-u leah-u commented Sep 25, 2025

I got the following error when trying to select an inet value with a netmask other than 32:

1> pgo:query("SELECT '192.168.0.1/24'::inet").
** exception error: no function clause matching 
                    pg_inet:decode(<<2,24,0,4,192,168,0,1>>,
                                   ...

It looks like pg_inet:decode currently matches on the third byte depending on the netmask value. This byte however is a flag indicating whether the type is inet or cidr1. Unless we want to return this type information, it should just be ignored.

I also changed the encoding for consistency, the is_cidr flag is ignored by postgres2.

Footnotes

  1. https://github.com/postgres/postgres/blob/e849bd551c323a384f2b14d20a1b7bfaa6127ed7/src/backend/utils/adt/network.c#L278

  2. https://github.com/postgres/postgres/blob/e849bd551c323a384f2b14d20a1b7bfaa6127ed7/src/backend/utils/adt/network.c#L217

@tsloughter
Copy link
Copy Markdown
Owner

Thanks! I'm guessing we should include that type information somehow, but for now we can do this as any other change would be backwards incompatible.

@tsloughter
Copy link
Copy Markdown
Owner

Looks like the prop test for cidr broke.

@leah-u leah-u force-pushed the fix-inet-decoding branch from ddb6bb5 to 5cf6552 Compare October 2, 2025 10:49
@leah-u
Copy link
Copy Markdown
Contributor Author

leah-u commented Oct 2, 2025

I changed my solution to just add decoding for inet values with a netmask.

@tsloughter
Copy link
Copy Markdown
Owner

How is this different from what is already there

pg_types/src/pg_inet.erl

Lines 35 to 38 in 5cf6552

decode(<<?INET, Mask:8, 1, ?IP_SIZE, Bin/binary>>, _) ->
{list_to_tuple(binary_to_list(Bin)), Mask};
decode(<<?INET6, Mask:8, 1, ?IP6_SIZE, Bin/binary>>, _) ->
{list_to_tuple([X || <<X:16>> <= Bin]), Mask};

@tsloughter
Copy link
Copy Markdown
Owner

Sorry about that! Merging now.

@tsloughter tsloughter merged commit ff34795 into tsloughter:main Dec 8, 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