Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vrt: Turn hdr_t into a structured type #4308

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

dridi
Copy link
Member

@dridi dridi commented Apr 2, 2025

Without changing the memory layout of our pseudo-pascal-string for header names, this structured alternative clarifies the semantics and removes error-prone pointer arithmetics spread out everywhere.

The new structure adds type safety to this specific use case.

It comes with a new static analyzer with the HDR() macro, and a CAST_HDR() macro that includes a check with the CHECK_HDR() macro also introduced by this patch series.

Solves #4304
Closes #3215

dridi added 7 commits April 1, 2025 15:21
It poses two problems alleviated by the previous char[] type:

- assigning a "string literal" that is considered const
- referencing a pointer in a static lookup table

The const problem is temporarily avoided with a char[] _H_Header static
variable only used to store the string the actual H_Header symbol will
point to.

The static table limitation is worked around, storing pointers to the
offending symbols. This workaround is permanent, because headers should
eventually take a new structured type that will pose the same problem.
Using the established hdr_t type definition.

Solves varnishcache#4304
It could be part of the well-known headers table, along with the h2
pseudo-header fields, but that would require an update of the perfect
hash lookup table.

The main goal so far is to formalize the hdr_t nature of this string.
It's a clunky macro, but its current shape is only a transition towards
something structured.
Without changing the memory layout of our pseudo-pascal-string for
header names, this structured alternative clarifies the semantics
and removes error-prone pointer arithmetics spread out everywhere.

The new structure adds type safety to this specific use case.

It comes with a new static analyzer with the HDR() macro, and a
CAST_HDR() macro that includes a check with the CHECK_HDR() macro
that was already present.

Closes varnishcache#3215
Copy link
Member

@nigoroll nigoroll left a comment

Choose a reason for hiding this comment

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

👍
all of bugwash approved

@bsdphk
Copy link
Contributor

bsdphk commented Apr 9, 2025

looks good to me

@dridi
Copy link
Member Author

dridi commented Apr 9, 2025

It comes with a new static analyzer with the HDR() macro,

I meant of course static initializer... I will fix that before merging.

@dridi
Copy link
Member Author

dridi commented Apr 9, 2025

Maybe before I get around to fixing the commit message someone™ could run flexelint and see how it fares?

@nigoroll
Copy link
Member

nigoroll commented Apr 9, 2025

run flexelint

Count   Type    Number  Text

4       Info    740     Unusual pointer cast (incompatible indirect types)
8       Info    754     local ___ member '___' (___) not referenced
1       Info    843     Variable '___' (___) could be declared as const
--- _.fl.old	2025-04-09 17:35:06.597578158 +0200
+++ _.fl	2025-04-09 17:36:50.401846332 +0200
@@ -41,6 +41,25 @@
 --- Module:   cache/cache_expire.c (C)
 
 --- Module:   cache/cache_fetch.c (C)
+File cache/cache_fetch.c, Line 67
+                                          _
+static hdr_t H_X_Varnish = HDR("X-Varnish");
+    Info 740: Unusual pointer cast (incompatible indirect types)
+
+    --- Wrap-up for Module: cache/cache_fetch.c
+
+    Info 843: Variable 'H_X_Varnish' (line 67, file cache/cache_fetch.c) could
+    be declared as const
+File cache/cache_fetch.c, Line 67
+    Info 830: Location cited in prior message
+    Info 754: local struct member '_l' (line 67, file cache/cache_fetch.c) not
+    referenced
+File cache/cache_fetch.c, Line 67
+    Info 830: Location cited in prior message
+    Info 754: local struct member '_s' (line 67, file cache/cache_fetch.c) not
+    referenced
+File cache/cache_fetch.c, Line 67
+    Info 830: Location cited in prior message
 
 --- Module:   cache/cache_fetch_proc.c (C)
 
@@ -49,6 +68,45 @@
 --- Module:   cache/cache_hash.c (C)
 
 --- Module:   cache/cache_http.c (C)
+File cache/cache_http.c, Line 60
+                               _
+hdr_t H__Status	= HDR(":status");
+    Info 740: Unusual pointer cast (incompatible indirect types)
+File cache/cache_http.c, Line 61
+                              _
+hdr_t H__Proto	= HDR(":proto");
+    Info 740: Unusual pointer cast (incompatible indirect types)
+File cache/cache_http.c, Line 62
+                               _
+hdr_t H__Reason	= HDR(":reason");
+    Info 740: Unusual pointer cast (incompatible indirect types)
+
+    --- Wrap-up for Module: cache/cache_http.c
+
+    Info 754: local struct member '_l' (line 60, file cache/cache_http.c) not
+    referenced
+File cache/cache_http.c, Line 60
+    Info 830: Location cited in prior message
+    Info 754: local struct member '_s' (line 60, file cache/cache_http.c) not
+    referenced
+File cache/cache_http.c, Line 60
+    Info 830: Location cited in prior message
+    Info 754: local struct member '_l' (line 61, file cache/cache_http.c) not
+    referenced
+File cache/cache_http.c, Line 61
+    Info 830: Location cited in prior message
+    Info 754: local struct member '_s' (line 61, file cache/cache_http.c) not
+    referenced
+File cache/cache_http.c, Line 61
+    Info 830: Location cited in prior message
+    Info 754: local struct member '_l' (line 62, file cache/cache_http.c) not
+    referenced
+File cache/cache_http.c, Line 62
+    Info 830: Location cited in prior message
+    Info 754: local struct member '_s' (line 62, file cache/cache_http.c) not
+    referenced
+File cache/cache_http.c, Line 62
+    Info 830: Location cited in prior message
 
 --- Module:   cache/cache_lck.c (C)
 
@@ -990,3 +1048,6 @@
 
 Count   Type    Number  Text
 
+4       Info    740     Unusual pointer cast (incompatible indirect types)
+8       Info    754     local ___ member '___' (___) not referenced
+1       Info    843     Variable '___' (___) could be declared as const

@nigoroll
Copy link
Member

nigoroll commented Apr 9, 2025

I am working on a suggestion to make Flexelint happy(er).
edit: For now, I have not found a nice way for the "local member not referenced" warning (not just silencing it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants