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

vdef: Add TFOREACH() macro for txt #4194

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bin/varnishd/http2/cache_http2_hpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ h2h_checkhdr(struct vsl_log *vsl, txt nm, txt val)

l = vmin_t(int, Tlen(nm) + 2 + Tlen(val), 20);
state = FLD_NAME_FIRST;
for (p = nm.b; p < nm.e; p++) {
Tforeach(p, nm) {
switch(state) {
case FLD_NAME_FIRST:
state = FLD_NAME;
Expand All @@ -101,7 +101,7 @@ h2h_checkhdr(struct vsl_log *vsl, txt nm, txt val)
}

state = FLD_VALUE_FIRST;
for (p = val.b; p < val.e; p++) {
Tforeach(p, val) {
switch(state) {
case FLD_VALUE_FIRST:
if (vct_issp(*p)) {
Expand Down Expand Up @@ -174,7 +174,7 @@ h2h_addhdr(struct http *hp, struct h2h_decode *d)
disallow_empty = 1;

/* Check HTTP token */
for (p = hdr.b; p < hdr.e; p++) {
Tforeach(p, hdr) {
if (!vct_istchar(*p))
return (H2SE_PROTOCOL_ERROR);
}
Expand All @@ -192,7 +192,7 @@ h2h_addhdr(struct http *hp, struct h2h_decode *d)
}

/* Path cannot contain LWS or CTL */
for (p = hdr.b; p < hdr.e; p++) {
Tforeach(p, hdr) {
if (vct_islws(*p) || vct_isctl(*p))
return (H2SE_PROTOCOL_ERROR);
}
Expand All @@ -206,7 +206,7 @@ h2h_addhdr(struct http *hp, struct h2h_decode *d)
disallow_empty = 1;

/* Check HTTP token */
for (p = val.b; p < val.e; p++) {
Tforeach(p, val) {
if (!vct_istchar(*p))
return (H2SE_PROTOCOL_ERROR);
}
Expand Down
1 change: 1 addition & 0 deletions include/vdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ typedef struct {
#define Tlen(t) (pdiff((t).b, (t).e))
#define Tstr(s) (/*lint -e(446)*/ (txt){(s), (s) + strlen(s)})
#define Tstrcmp(t, s) (strncmp((t).b, (s), Tlen(t)))
#define Tforeach(c, t) for ((c) = (t).b; (c) < (t).e; (c)++)

/* #3020 dummy definitions until PR is merged*/
#define LIKELY(x) (x)
Expand Down
14 changes: 14 additions & 0 deletions tools/coccinelle/tforeach.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* This patch turns manual loops over a txt into foreach loops */

@@
iterator name Tforeach;
typedef txt;
expression c;
txt t;
@@

- for (c = t.b; c < t.e; c++)
+ Tforeach(c, t)
{
...
}