Skip to content

Commit

Permalink
lib: avoid fallthrough cases in switch statements
Browse files Browse the repository at this point in the history
Commit b5a434f inhibits the warning
on implicit fallthrough cases, since the current coding of indicating
fallthrough with comments is falling out of fashion with new compilers.
This attempts to make the issue smaller by rewriting fallthroughs to no
longer fallthrough, via either breaking the cases or turning switch
statements into if statements.

  lib/content_encoding.c: the fallthrough codepath is simply copied
    into the case as it's a single line.
  lib/http_ntlm.c: the fallthrough case skips a state in the state-
    machine and fast-forwards to NTLMSTATE_LAST. Do this before the
    switch statement instead to set up the states that we actually
    want.
  lib/http_proxy.c: the fallthrough is just falling into exiting the
    switch statement which can be done easily enough in the case.
  lib/mime.c: switch statement rewritten as if statement.
  lib/pop3.c: the fallthrough case skips to the next state in the
    statemachine, do this explicitly instead.
  lib/urlapi.c: switch statement rewritten as if statement.
  lib/vssh/wolfssh.c: the fallthrough cases fast-forwards the state
    machine, do this by running another iteration of the switch
    statement instead.
  lib/vtls/gtls.c: switch statement rewritten as if statement.
  lib/vtls/nss.c: the fallthrough codepath is simply copied into the
    case as it's a single line. Also twiddle a comment to not be
    inside a non-brace if statement.

Closes: #xxxx
See-also: curl#7295
  • Loading branch information
danielgustafsson committed Jun 29, 2021
1 parent 8ccc066 commit ff12fec
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 65 deletions.
3 changes: 2 additions & 1 deletion lib/content_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ static CURLcode inflate_stream(struct Curl_easy *data,
}
zp->zlib_init = ZLIB_UNINIT; /* inflateEnd() already called. */
}
/* FALLTHROUGH */
result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
break;
default:
result = exit_zlib(data, z, &zp->zlib_init, process_zlib_error(data, z));
break;
Expand Down
11 changes: 6 additions & 5 deletions lib/http_ntlm.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
#endif

Curl_bufref_init(&ntlmmsg);

/* connection is already authenticated, don't send a header in future
* requests so do directly to NTLMSTATE_LAST */
if(*state == NTLMSTATE_TYPE3)
*state = NTLMSTATE_LAST;

switch(*state) {
case NTLMSTATE_TYPE1:
default: /* for the weird cases we (re)start here */
Expand Down Expand Up @@ -246,11 +252,6 @@ CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
}
break;

case NTLMSTATE_TYPE3:
/* connection is already authenticated,
* don't send a header in future requests */
*state = NTLMSTATE_LAST;
/* FALLTHROUGH */
case NTLMSTATE_LAST:
Curl_safefree(*allocuserpwd);
authp->done = TRUE;
Expand Down
3 changes: 2 additions & 1 deletion lib/http_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,8 @@ static CURLcode CONNECT(struct Curl_easy *data,
h->write_waker = NULL;
}
}
/* FALLTHROUGH */
break;

default:
break;
}
Expand Down
13 changes: 7 additions & 6 deletions lib/mime.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,13 @@ static size_t encoder_base64_read(char *buffer, size_t size, bool ateof,
/* Buffered data size can only be 0, 1 or 2. */
ptr[2] = ptr[3] = '=';
i = 0;
switch(st->bufend - st->bufbeg) {
case 2:
i = (st->buf[st->bufbeg + 1] & 0xFF) << 8;
/* FALLTHROUGH */
case 1:

/* If there is buffered data */
if(st->bufend != st->bufbeg) {

if(st->bufend - st->bufbeg == 2)
i = (st->buf[st->bufbeg + 1] & 0xFF) << 8;

i |= (st->buf[st->bufbeg] & 0xFF) << 16;
ptr[0] = base64[(i >> 18) & 0x3F];
ptr[1] = base64[(i >> 12) & 0x3F];
Expand All @@ -476,7 +478,6 @@ static size_t encoder_base64_read(char *buffer, size_t size, bool ateof,
}
cursize += 4;
st->pos += 4;
break;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/pop3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,9 @@ static CURLcode pop3_statemachine(struct Curl_easy *data,
break;

case POP3_QUIT:
/* fallthrough, just stop! */
state(data, POP3_STOP);
break;

default:
/* internal error */
state(data, POP3_STOP);
Expand Down
48 changes: 23 additions & 25 deletions lib/urlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,23 @@ static size_t strlen_url(const char *url, bool relative)
continue;
}

switch(*ptr) {
case '?':
left = FALSE;
/* FALLTHROUGH */
default:
if(urlchar_needs_escaping(*ptr))
newlen += 2;
newlen++;
break;
case ' ':
if(*ptr == ' ') {
if(left)
newlen += 3;
else
newlen++;
break;
continue;
}

if (*ptr == '?')
left = FALSE;

if(urlchar_needs_escaping(*ptr))
newlen += 2;

newlen++;
}

return newlen;
}

Expand Down Expand Up @@ -202,28 +202,26 @@ static void strcpy_url(char *output, const char *url, bool relative)
continue;
}

switch(*iptr) {
case '?':
left = FALSE;
/* FALLTHROUGH */
default:
if(urlchar_needs_escaping(*iptr)) {
msnprintf(optr, 4, "%%%02x", *iptr);
optr += 3;
}
else
*optr++=*iptr;
break;
case ' ':
if(*iptr == ' ') {
if(left) {
*optr++='%'; /* add a '%' */
*optr++='2'; /* add a '2' */
*optr++='0'; /* add a '0' */
}
else
*optr++='+'; /* add a '+' here */
break;
continue;
}

if(*iptr == '?')
left = FALSE;

if(urlchar_needs_escaping(*iptr)) {
msnprintf(optr, 4, "%%%02x", *iptr);
optr += 3;
}
else
*optr++ = *iptr;
}
*optr = 0; /* null-terminate output buffer */

Expand Down
6 changes: 4 additions & 2 deletions lib/vssh/wolfssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ static CURLcode wssh_statemach_act(struct Curl_easy *data, bool *block)
switch(sshc->state) {
case SSH_INIT:
state(data, SSH_S_STARTUP);
/* FALLTHROUGH */
break;

case SSH_S_STARTUP:
rc = wolfSSH_connect(sshc->ssh_session);
if(rc != WS_SUCCESS)
Expand Down Expand Up @@ -838,7 +839,8 @@ static CURLcode wssh_statemach_act(struct Curl_easy *data, bool *block)
break;
}
state(data, SSH_SFTP_READDIR);
/* FALLTHROUGH */
break;

case SSH_SFTP_READDIR:
name = wolfSSH_SFTP_LS(sshc->ssh_session, sftp_scp->path);
if(!name)
Expand Down
39 changes: 17 additions & 22 deletions lib/vtls/gtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ gtls_connect_step1(struct Curl_easy *data,
const char * const hostname = SSL_HOST_NAME();
long * const certverifyresult = &SSL_SET_OPTION_LVALUE(certverifyresult);
const char *tls13support;
CURLcode result;

if(connssl->state == ssl_connection_complete)
/* to make us tolerant against being called more than once for the
Expand Down Expand Up @@ -557,31 +558,25 @@ gtls_connect_step1(struct Curl_easy *data,
/* Ensure +SRP comes at the *end* of all relevant strings so that it can be
* removed if a run-time error indicates that SRP is not supported by this
* GnuTLS version */
switch(SSL_CONN_CONFIG(version)) {
case CURL_SSLVERSION_TLSv1_3:
if(!tls13support) {
failf(data, "This GnuTLS installation does not support TLS 1.3");
return CURLE_SSL_CONNECT_ERROR;
}
/* FALLTHROUGH */
case CURL_SSLVERSION_DEFAULT:
case CURL_SSLVERSION_TLSv1:
case CURL_SSLVERSION_TLSv1_0:
case CURL_SSLVERSION_TLSv1_1:
case CURL_SSLVERSION_TLSv1_2: {
CURLcode result = set_ssl_version_min_max(data, &prioritylist,
tls13support);
if(result)
return result;
break;

if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2 ||
SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv3) {
failf(data, "GnuTLS does not support SSLv2 or SSLv3");
return CURLE_SSL_CONNECT_ERROR;
}

if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_TLSv1_3) {
if(!tls13support) {
failf(data, "This GnuTLS installation does not support TLS 1.3");
return curle_ssl_connect_error;
}
case CURL_SSLVERSION_SSLv2:
case CURL_SSLVERSION_SSLv3:
default:
failf(data, "GnuTLS does not support SSLv2 or SSLv3");
return CURLE_SSL_CONNECT_ERROR;
}

/* At this point we know we have a supported TLS version, so set it */
result = set_ssl_version_min_max(data, &prioritylist, tls13support);
if(result)
return result;

#ifdef HAVE_GNUTLS_SRP
/* Only add SRP to the cipher list if SRP is requested. Otherwise
* GnuTLS will disable TLS 1.3 support. */
Expand Down
5 changes: 3 additions & 2 deletions lib/vtls/nss.c
Original file line number Diff line number Diff line change
Expand Up @@ -2250,10 +2250,11 @@ static CURLcode nss_connect_common(struct Curl_easy *data,
case CURLE_OK:
break;
case CURLE_AGAIN:
/* CURLE_AGAIN in non-blocking mode is not an error */
if(!blocking)
/* CURLE_AGAIN in non-blocking mode is not an error */
return CURLE_OK;
/* FALLTHROUGH */
else
return result;
default:
return result;
}
Expand Down

0 comments on commit ff12fec

Please sign in to comment.