Skip to content

HTTP: fix empty-body warning#12262

Closed
Gottox wants to merge 1 commit into
curl:masterfrom
Gottox:fix/Wempty-body
Closed

HTTP: fix empty-body warning#12262
Gottox wants to merge 1 commit into
curl:masterfrom
Gottox:fix/Wempty-body

Conversation

@Gottox

@Gottox Gottox commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

This change fixes a compiler warning with gcc-12.2.0 when -DCURL_DISABLE_BEARER_AUTH=ON is used.

/home/tox/src/curl/lib/http.c: In function 'Curl_http_input_auth':
/home/tox/src/curl/lib/http.c:1147:12: warning: suggest braces around empty body in an 'else' statement [-Wempty-body]
 1147 |            ;
      |            ^

This is useful as it fixes an issue when libcurl is compiled with -Werror.

@github-actions github-actions Bot added the HTTP label Nov 3, 2023
@vszakats

vszakats commented Nov 3, 2023

Copy link
Copy Markdown
Member

Ref, this fixes one of the 4 warnings reported in #12228.

Comment thread lib/http.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seeing this is an unusual construct for curl code, can you add a comment here explaining why plain ; is not enough?

@Gottox Gottox Nov 4, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Simply put it: Compilers. gcc warns about orphaned ; in the source code. This warning is also enabled by the -Wextra flag. I'm not sure about the reason, I would guess it prevents programming errors like this:

if (foo);
   do_stuff();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, and I want that as a comment in the code so that future readers understand that is the reason why this construct is used...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Misread, will add!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Tbh, I personally find this macro spaghetti quite hard to understand. Would you be fine if I refactor this into static functions too?

@Gottox Gottox Nov 5, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Something like this: (pseudocode)

static bool handle_ntlm(const char *auth, ...) {
#ifdef USE_NTLM
  if (!checkprefix("Negotiate", auth) || !is_valid_auth_separator(auth[9])) {
    return false;
  }
  // NTLM auth code
  return true;
#else
  (void)auth;
  // ...
  return false;
#endif
}

// ...

CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
                              const char *auth) /* the first non-space */
{
// ...
  if (handle_ntlm(auth, ...)) {
  } else if (handle_basic(auth, ...)) {
  } else if (handle_...(auth, ...)) {
  } else ...

The compiler would still eliminate empty functions, but I find it less noisy to read as the macros don't need to workaround syntactic issues. Do you think this could help readability?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes I think it would. Reducing ifdef-mazes is a worthy effort.

@Gottox Gottox Nov 5, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The PR is finished as is, I'll open a new one for the refactoring. (I know myself, my attention is a squirrel.)

This change fixes a compiler warning with gcc-12.2.0 when
`-DCURL_DISABLE_BEARER_AUTH=ON` is used.

    /home/tox/src/curl/lib/http.c: In function 'Curl_http_input_auth':
    /home/tox/src/curl/lib/http.c:1147:12: warning: suggest braces around empty body in an 'else' statement [-Wempty-body]
     1147 |            ;
          |            ^
@bagder bagder closed this in 46878b9 Nov 5, 2023
@Gottox Gottox deleted the fix/Wempty-body branch November 6, 2023 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants