Skip to content

IPv4 parsing sometimes succeeds on invalid inputs #90544

@al13n321

Description

@al13n321

parseIPv4SSE doesn't check that dotmask is valid (it would add noticeable overhead). Easy to find counterexamples by trying random strings consisting of '1' and '.':

SELECT
    arrayStringConcat(arrayMap(i -> if((rand((number, i)) % 3) = 0, '.', '1'), range(15))) AS s,
    IPv4NumToString(IPv4StringToNumOrDefault(s)) AS ip
FROM numbers(1000)
WHERE ip != '0.0.0.0'
LIMIT 10

Query id: 1249091b-2c7c-40fc-979e-2bf71ce8c01a

    ┌─s───────────────┬─ip───────────┐
 1. │ 1111111.111111111.11.1.12. │ 111111.111.111. │ 1.1.11.13. │ 1111111111.1.111.1.1.114. │ 11111111.111.1111.111.1.115. │ 111.1111.111.111.1.1.16. │ 1111111.111111111.11.1.17. │ 1111111.111111111.11.1.18. │ 111111111111.1111.11.111.119. │ 1.111111111.1.11.111.11.110. │ 111.1111111.1.1111.1.11.1   │
    └─────────────────┴──────────────┘

Sometimes the result for one row depends on the next row:

:) select IPv4NumToString(IPv4StringToNumOrDefault(x)) from system.one array join ['24', '5.123.234'] as x

SELECT IPv4NumToString(IPv4StringToNumOrDefault(x))
FROM system.one
ARRAY JOIN ['24', '5.123.234'] AS x

Query id: 38b0f4c1-e81c-45fc-b3e9-67519fb582d0

   ┌─IPv4NumToStr⋯Default(x))─┐
1. │ 245.1.3.2342. │ 0.0.0.0                  │
   └──────────────────────────┘

2 rows in set. Elapsed: 0.002 sec. 

:) select IPv4NumToString(IPv4StringToNumOrDefault(x)) from system.one array join ['24'] as x

SELECT IPv4NumToString(IPv4StringToNumOrDefault(x))
FROM system.one
ARRAY JOIN ['24'] AS x

Query id: cd961c40-c679-4d3d-b3a4-69aa4c041093

   ┌─IPv4NumToStr⋯Default(x))─┐
1. │ 0.0.0.0                  │
   └──────────────────────────┘

1 row in set. Elapsed: 0.002 sec. 

I guess we should make it a little slower and validate the input?

(EDIT: Ok, it turned out easier to fix than expected, didn't have to create an issue.)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions