Skip to contentSkip to conversation

imap-send: include strbuf.h

PATCH In review Christian Hesse 5 participants 20 messages May 17, 2023 – May 18, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 imap-send: include strbuf.h

From: Christian Hesse <[email protected]>

We use xstrfmt() here, so let's include the header file.

---
 imap-send.c | 1 +
 1 file changed, 1 insertion(+)
imap-send.c +1 -0
index a62424e90a..7f5426177a 100644
@@ -29,6 +29,7 @@
 #include "run-command.h"
 #include "parse-options.h"
 #include "setup.h"
+#include "strbuf.h"
 #include "wrapper.h"
 #if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
 typedef void *SSL;
Signature · 1 line
2.40.1
#1 lore raw
Quoted 8 lines · Christian Hesse

From: Christian Hesse <[email protected]>

We use xstrfmt() here, so let's include the header file.

---
 imap-send.c | 1 +
 1 file changed, 1 insertion(+)

Puzzled. For me Git 2.41-rc0 builds as-is without this change just fine, it seems. I know there are many header file shuffling patches flying around, and I have seen some of them, but is this a fix for one of these patches?

Thanks.

Quoted 13 lines
imap-send.c +1 -0
index a62424e90a..7f5426177a 100644
@@ -29,6 +29,7 @@
 #include "run-command.h"
 #include "parse-options.h"
 #include "setup.h"
+#include "strbuf.h"
 #include "wrapper.h"
 #if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
 typedef void *SSL;
#2 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Christian Hesse

From: Christian Hesse <[email protected]>

We use xstrfmt() here, so let's include the header file.

Quoted 7 lines
Quoted 4 lines
---
 imap-send.c | 1 +
 1 file changed, 1 insertion(+)

Puzzled. For me Git 2.41-rc0 builds as-is without this change just fine, it seems.

It will fail to build for ancient versions of curl (pre-7.34.0, which was released in 2013), or if you build with NO_CURL=1.

Quoted 2 lines

I know there are many header file shuffling patches flying around, and I have seen some of them, but is this a fix for one of these patches?

Similar to [1], this bisects to ba3d1c73da (treewide: remove unnecessary cache.h includes, 2023-02-24).

Thanks,
Taylor

[1]: https://lore.kernel.org/git/[email protected]/

#3 lore raw
Quoted 12 lines · Taylor Blau
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Christian Hesse

From: Christian Hesse <[email protected]>

We use xstrfmt() here, so let's include the header file.

---
 imap-send.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 5 lines
Quoted 2 lines

Puzzled. For me Git 2.41-rc0 builds as-is without this change just fine, it seems.

It will fail to build for ancient versions of curl (pre-7.34.0, which was released in 2013), or if you build with NO_CURL=1.

xstrfmt() is used at exactly one place, inside "#ifndef NO_OPENSSL", in the implementation of the static function cram().

Ah, the mention of that function was a huge red herring. There are tons of strbuf API calls in the file outside any conditional compilation, and where it inherits the include from is "http.h", that is conditionally included.

OK, so the fix seems to make sense, but the justification for the change needs to be rewritten, I think.

We make liberal use of the strbuf API functions and types, but the inclusion of <strbuf.h> comes indirectly by including <http.h>, which does not happen if you build with NO_CURL.

or something like that?

Thanks.

#8 lore raw
In reply to Taylor Blau
Quoted 5 lines · Taylor Blau
Quoted 2 lines

I know there are many header file shuffling patches flying around, and I have seen some of them, but is this a fix for one of these patches?

Similar to [1], this bisects to ba3d1c73da (treewide: remove unnecessary cache.h includes, 2023-02-24).

I'm looking through other files to see if we have other uses of the strbuf API that are hidden behind a #define without a corresponding #include "strbuf.h".

Here's the (gross) script I wrote up to check:

    git grep -l -e '[^_]xstrdup(' -e 'strbuf_[a-z0-9A-Z_]*(' \*.c |
    while read f
    do
        if ! gcc -I $(pwd) -E $f | grep -q 'struct strbuf {'
        then
            echo "==> $f NOT OK";
        fi
    done

Here's the list:

==> compat/fsmonitor/fsm-listen-darwin.c NOT OK
==> compat/mingw.c NOT OK
==> contrib/credential/osxkeychain/git-credential-osxkeychain.c NOT OK ==> pager.c NOT OK
==> refs/iterator.c NOT OK
==> refs/ref-cache.c NOT OK
==> string-list.c NOT OK
==> t/helper/test-mktemp.c NOT OK

The ones in compat are OK to ignore since they both fail to compile on my non-Windows machine (I am missing the <dispatch/dispatch.h> and <windows.h> headers, respectively).

The one in contrib is fine to ignore, since it has its own definition of xstrdup().

pager.c is OK, since it only needs xstrdup(), not any other parts of the strbuf API. It gets a declaration of xstrdup() from git-compat-util.h refs/iterator.c, refs/ref-cache.c, string-list.c, and t/helper/test-mktemp.c are all OK for the same reason.

So I think that this is the only spot we need to worry about.

Thanks,
Taylor

#4 lore raw
In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano

OK, so the fix seems to make sense, but the justification for the change needs to be rewritten, I think.

We make liberal use of the strbuf API functions and types, but the inclusion of <strbuf.h> comes indirectly by including <http.h>, which does not happen if you build with NO_CURL.

or something like that?

Agreed. Here's a patch that summarizes my investigation. Thanks again, Christian, for reporting!

--- 8< ---

Subject: [PATCH] imap-send.c: fix compilation under NO_CURL and ancient
versions

When building imap-send.c with an ancient (pre-7.34.0, which was released in 2013) version of curl, or with NO_CURL defined, Git 2.41.0-rc0 fails to compile imap-send.c, i.e.

    $ make NO_CURL=1 imap-send.o

This is similar to 52c0f3318d (run-command.c: fix missing include under NO_PTHREADS, 2023-05-16), but the bisection points at ba3d1c73da (treewide: remove unnecessary cache.h includes, 2023-02-24) instead.

The trivial fix is to include "strbuf.h" unconditionally, which is a noop for most builds, and saves us otherwise.

To check for other *.c files that might suffer from the same issue, we can run:

    git grep -l -e '[^_]xstrdup(' -e 'strbuf_[a-z0-9A-Z_]*(' \*.c |
    while read f
    do
        if ! gcc -I $(pwd) -E $f | grep -q 'struct strbuf {'
        then
            echo "==> $f NOT OK";
        fi
    done

...which runs the preprocessor on (roughly) all C source files that call xstrdup() or use the strbuf API. The resulting list looks (on my machine) lile:

    ==> compat/fsmonitor/fsm-listen-darwin.c NOT OK
    ==> compat/mingw.c NOT OK
    ==> contrib/credential/osxkeychain/git-credential-osxkeychain.c NOT OK
    ==> pager.c NOT OK
    ==> refs/iterator.c NOT OK
    ==> refs/ref-cache.c NOT OK
    ==> string-list.c NOT OK
    ==> t/helper/test-mktemp.c NOT OK

The ones in compat are OK to ignore since they both fail to compile on my non-Windows machine (I am missing the <dispatch/dispatch.h> and <windows.h> headers, respectively).

The one in contrib is fine to ignore, since it has its own definition of xstrdup().

pager.c is OK, since it only needs xstrdup(), not any other parts of the strbuf API. It gets a declaration of xstrdup() from git-compat-util.h refs/iterator.c, refs/ref-cache.c, string-list.c, and t/helper/test-mktemp.c are all OK for the same reason.

So this is the only spot that needs fixing.

---
 imap-send.c | 1 +
 1 file changed, 1 insertion(+)
imap-send.c +1 -0
index a62424e90a..7f5426177a 100644
@@ -29,6 +29,7 @@
 #include "run-command.h"
 #include "parse-options.h"
 #include "setup.h"
+#include "strbuf.h"
 #include "wrapper.h"
 #if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
 typedef void *SSL;

--
2.41.0.rc0.1.g01bd045298

#9 lore raw
In reply to Taylor Blau
Quoted 10 lines · Taylor Blau

Here's the (gross) script I wrote up to check:

    git grep -l -e '[^_]xstrdup(' -e 'strbuf_[a-z0-9A-Z_]*(' \*.c |
    while read f
    do
        if ! gcc -I $(pwd) -E $f | grep -q 'struct strbuf {'
        then
            echo "==> $f NOT OK";
        fi
    done

I am a bit puzzled.

What does the above prove, more than what your regular compilation that does not fail, tells us? Doesn't -E expand recursively, so for the case of imap-send.c, with your usual configuration, wouldn't it have grabbed "struct strbuf" via inclusion of <http.h> indirectly anyway?

Quoted 24 lines

Here's the list:

==> compat/fsmonitor/fsm-listen-darwin.c NOT OK
==> compat/mingw.c NOT OK
==> contrib/credential/osxkeychain/git-credential-osxkeychain.c NOT OK ==> pager.c NOT OK
==> refs/iterator.c NOT OK
==> refs/ref-cache.c NOT OK
==> string-list.c NOT OK
==> t/helper/test-mktemp.c NOT OK

The ones in compat are OK to ignore since they both fail to compile on my non-Windows machine (I am missing the <dispatch/dispatch.h> and <windows.h> headers, respectively).

The one in contrib is fine to ignore, since it has its own definition of xstrdup().

pager.c is OK, since it only needs xstrdup(), not any other parts of the strbuf API. It gets a declaration of xstrdup() from git-compat-util.h refs/iterator.c, refs/ref-cache.c, string-list.c, and t/helper/test-mktemp.c are all OK for the same reason.

So I think that this is the only spot we need to worry about.

#10 lore raw
Quoted 4 lines · Junio C Hamano
Quoted 1 line
        if ! gcc -I $(pwd) -E $f | grep -q 'struct strbuf {'

...
What does the above prove, more than what your regular compilation that does not fail, tells us?

It is actually worse than that, isn't it? This does not even use the definition in the config.mak.uname, so it is not even matching your build environment.

I am uncomfortable to use this as an explanation of what due diligence we did to convince ourselves that this fix should cover all similar issues. Perhaps I am grossly misunderstanding what your investigation did?

Thanks.

#11 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Junio C Hamano
Quoted 1 line
        if ! gcc -I $(pwd) -E $f | grep -q 'struct strbuf {'

...
What does the above prove, more than what your regular compilation that does not fail, tells us?

Quoted 8 lines

It is actually worse than that, isn't it? This does not even use the definition in the config.mak.uname, so it is not even matching your build environment.

I am uncomfortable to use this as an explanation of what due diligence we did to convince ourselves that this fix should cover all similar issues. Perhaps I am grossly misunderstanding what your investigation did?

Oof, yes, you are right:

    diff -u \
      <(gcc -I . -E imap-send.c) \
      <(gcc -DNO_CURL=1 -I . -E imap-send.c)

How *should* we test this?

Thanks,
Taylor

#12 lore raw
Quoted 17 lines · Taylor Blau
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Junio C Hamano
Quoted 1 line
        if ! gcc -I $(pwd) -E $f | grep -q 'struct strbuf {'

...
What does the above prove, more than what your regular compilation that does not fail, tells us?

Quoted 8 lines

It is actually worse than that, isn't it? This does not even use the definition in the config.mak.uname, so it is not even matching your build environment.

I am uncomfortable to use this as an explanation of what due diligence we did to convince ourselves that this fix should cover all similar issues. Perhaps I am grossly misunderstanding what your investigation did?

Quoted 7 lines

Oof, yes, you are right:

   diff -u \
     <(gcc -I . -E imap-send.c) \
     <(gcc -DNO_CURL=1 -I . -E imap-send.c)

How *should* we test this?

I hope not by using gcc, which is not currently a dependency. Using the C preprocessor directly might help in a more general sense, but you probably will need a knob for some compilers to work.

#15 lore raw
In reply to Taylor Blau
Quoted 17 lines · Taylor Blau
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Junio C Hamano
Quoted 1 line
        if ! gcc -I $(pwd) -E $f | grep -q 'struct strbuf {'

...
What does the above prove, more than what your regular compilation that does not fail, tells us?

Quoted 8 lines

It is actually worse than that, isn't it? This does not even use the definition in the config.mak.uname, so it is not even matching your build environment.

I am uncomfortable to use this as an explanation of what due diligence we did to convince ourselves that this fix should cover all similar issues. Perhaps I am grossly misunderstanding what your investigation did?

Quoted 7 lines

Oof, yes, you are right:

    diff -u \
      <(gcc -I . -E imap-send.c) \
      <(gcc -DNO_CURL=1 -I . -E imap-send.c)

How *should* we test this?

My inclination is punt and simply do not to claim that we have done a good due diligence to ensure with all permutations of ifdef we are including necessary headers.

#13 lore raw
In reply to [email protected]
Quoted 6 lines · <[email protected]> writes:
Quoted 6 lines

Oof, yes, you are right:

   diff -u \
     <(gcc -I . -E imap-send.c) \
     <(gcc -DNO_CURL=1 -I . -E imap-send.c)
Quoted 6 lines
Quoted 1 line

How *should* we test this?

I hope not by using gcc, which is not currently a dependency. Using the C preprocessor directly might help in a more general sense, but you probably will need a knob for some compilers to work.

I am not going to suggest trying all permutations of CPP macros to make sure we cover all the #ifdef'ed sections, but -E to show CPP output is pretty common feature not limited to "gcc", so if we were to do that, we'd very likely use the usual $(CC) Makefile macro to invoke such a test.

#14 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · <[email protected]> writes:
Quoted 6 lines

Oof, yes, you are right:

   diff -u \
     <(gcc -I . -E imap-send.c) \
     <(gcc -DNO_CURL=1 -I . -E imap-send.c)
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

How *should* we test this?

I hope not by using gcc, which is not currently a dependency. Using the C preprocessor directly might help in a more general sense, but you probably will need a knob for some compilers to work.

I am not going to suggest trying all permutations of CPP macros to make

sure we

Quoted 1 line

cover all the #ifdef'ed sections, but -E to show CPP output is pretty

common feature

Quoted 1 line

not limited to "gcc", so if we were to do that, we'd very likely use the

usual $(CC)

Quoted 1 line

Makefile macro to invoke such a test.

-E would work for me, but I do recall other platforms that would not (but can't place them at the moment). No objection from me, but it still might be useful to have a variable, like CPPFLAGS=-E (by default), that might help others, in config.mak.uname.

#6 lore raw
In reply to Christian Hesse

Christian Hesse <[email protected]> on Wed, 2023/05/17 22:12:

Quoted 11 lines
Quoted 8 lines

OK, so the fix seems to make sense, but the justification for the change needs to be rewritten, I think.

We make liberal use of the strbuf API functions and types, but the inclusion of <strbuf.h> comes indirectly by including <http.h>, which does not happen if you build with NO_CURL.

or something like that?

Fine with me!
Do you want me to re-send the patch or do you modify this on the fly?

Found it in next branch already. Thanks a lot!

Signature · 3 lines
main(a){char*c=/*    Schoene Gruesse                         */"B?IJj;MEH"
"CX:;",b;for(a/*    Best regards             my address:    */=0;b=c[a++];)
putchar(b-1/(/*    Chris            cc -ox -xc - && ./x    */b/42*2-3)*42);}
#5 lore raw
In reply to Junio C Hamano

Junio C Hamano <[email protected]> on Wed, 2023/05/17 09:19:

Quoted 9 lines
Quoted 7 lines · Taylor Blau
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Christian Hesse

From: Christian Hesse <[email protected]>

We use xstrfmt() here, so let's include the header file.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
---
 imap-send.c | 1 +
 1 file changed, 1 insertion(+)  

Puzzled. For me Git 2.41-rc0 builds as-is without this change just fine, it seems.

I prepared cgit to build with libgit.a 2.41.0-rc0. While cgit itself builds fine (with some justifications of course), building git for the test suite failed.

Quoted 2 lines
Quoted 2 lines

It will fail to build for ancient versions of curl (pre-7.34.0, which was released in 2013), or if you build with NO_CURL=1.

Indeed we have NO_CURL=1 in cgit's Makefile...

Quoted 4 lines

xstrfmt() is used at exactly one place, inside "#ifndef NO_OPENSSL", in the implementation of the static function cram().

Ah, the mention of that function was a huge red herring.

Well, the warning about implicit declaration of xstrfmt() was this one that popped up... :)
Sorry for the confusion.

Quoted 5 lines

There are
tons of strbuf API calls in the file outside any conditional compilation, and where it inherits the include from is "http.h", that is conditionally included.

Quoted 8 lines

OK, so the fix seems to make sense, but the justification for the change needs to be rewritten, I think.

We make liberal use of the strbuf API functions and types, but the inclusion of <strbuf.h> comes indirectly by including <http.h>, which does not happen if you build with NO_CURL.

or something like that?

Fine with me!
Do you want me to re-send the patch or do you modify this on the fly?

Quoted 1 line

Thanks.

Signature · 3 lines
main(a){char*c=/*    Schoene Gruesse                         */"B?IJj;MEH"
"CX:;",b;for(a/*    Best regards             my address:    */=0;b=c[a++];)
putchar(b-1/(/*    Chris            cc -ox -xc - && ./x    */b/42*2-3)*42);}
#16 lore raw
In reply to Junio C Hamano
Quoted 25 lines · Junio C Hamano
Quoted 23 lines · Taylor Blau
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Junio C Hamano
Quoted 1 line
        if ! gcc -I $(pwd) -E $f | grep -q 'struct strbuf {'

...
What does the above prove, more than what your regular compilation that does not fail, tells us?

Quoted 8 lines

It is actually worse than that, isn't it? This does not even use the definition in the config.mak.uname, so it is not even matching your build environment.

I am uncomfortable to use this as an explanation of what due diligence we did to convince ourselves that this fix should cover all similar issues. Perhaps I am grossly misunderstanding what your investigation did?

Oof, yes, you are right:

    diff -u \
      <(gcc -I . -E imap-send.c) \
      <(gcc -DNO_CURL=1 -I . -E imap-send.c)
Quoted 5 lines
Quoted 1 line

How *should* we test this?

My inclination is punt and simply do not to claim that we have done a good due diligence to ensure with all permutations of ifdef we are including necessary headers.

I think that's the best course of action, too. I see that it's already on 'next', thanks.

Thanks,
Taylor

#7 lore raw
In reply to Christian Hesse
Quoted 8 lines · Christian Hesse

Christian Hesse <[email protected]> on Wed, 2023/05/17 22:12:

Quoted 7 lines
Quoted 7 lines

OK, so the fix seems to make sense, but the justification for the change needs to be rewritten, I think.

We make liberal use of the strbuf API functions and types, but the inclusion of <strbuf.h> comes indirectly by including <http.h>, which does not happen if you build with NO_CURL.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

or something like that?

Fine with me!
Do you want me to re-send the patch or do you modify this on the fly?

Found it in next branch already. Thanks a lot!

We made moderate amount of these kind of header shuffling this cycle, and it is inevitable to encounter a gotcha like this when building with anything less than the most common configurations.

Thanks for reporting and fixing; very much appreciated.

#17 lore raw
In reply to Taylor Blau
Quoted 6 lines · Taylor Blau
Quoted 3 lines

My inclination is punt and simply do not to claim that we have done a good due diligence to ensure with all permutations of ifdef we are including necessary headers.

I think that's the best course of action, too. I see that it's already on 'next', thanks.

Yeah, I am actuall hoping that somebody clever, with time, comes up with a systematic way to give us better coverage, but until then, I think it is better to honestly record where we are to future developers.

Thanks.

#18 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Taylor Blau
Quoted 3 lines

My inclination is punt and simply do not to claim that we have done a good due diligence to ensure with all permutations of ifdef we are including necessary headers.

Quoted 7 lines
Quoted 2 lines

I think that's the best course of action, too. I see that it's already on 'next', thanks.

Yeah, I am actuall hoping that somebody clever, with time, comes up with a systematic way to give us better coverage, but until then, I think it is better to honestly record where we are to future developers.

I faced a similar issue with the -Wunused-parameter patches. Just when I thought I had everything annotated, I'd find some obscure Makefile knob that compiled new code (or even in one case disabled some code that used a variable!).

I never came up with a good solution, but relying on CI helped, since it just builds more of the combinations. Obviously that didn't catch this case. We could try to hit more platforms / combinations of knobs in CI, but there are diminishing returns on the compute time.

But at least in this case, the old "if it is important, somebody will build it and report the problem" line of thinking worked out. So maybe that is enough.

I guess maybe that was more philosophical than helpful. ;)

-Peff

#19 lore raw
Quoted 7 lines · Jeff King
Quoted 4 lines · Junio C Hamano

Yeah, I am actuall hoping that somebody clever, with time, comes up with a systematic way to give us better coverage, but until then, I think it is better to honestly record where we are to future developers.

Quoted 8 lines

I faced a similar issue with the -Wunused-parameter patches. Just when I thought I had everything annotated, I'd find some obscure Makefile knob that compiled new code (or even in one case disabled some code that used a variable!).
...
But at least in this case, the old "if it is important, somebody will build it and report the problem" line of thinking worked out. So maybe that is enough.

Ah, I guess the debugging situation is quite similar with that topic. Thanks for your insight.