Skip to content

Commit

Permalink
build: fix compiling with GCC 4.x versions
Browse files Browse the repository at this point in the history
- silence false positive picky warnings.
- avoid "possible noreturn" warnings for standalone tests and examples.
- fix to compile without `#pragma GCC diagnostic push` support.
- fix "#pragma GCC diagnostic not allowed inside functions".

Prerequisite for curl#15975 that needs GCC 4.4 for the latest pre-built
CeGCC/mingw32ce toolchain for Windows CE.

Cherry-picked from curl#15975
Closes curl#16062
  • Loading branch information
vszakats committed Jan 22, 2025
1 parent 96843f4 commit 7e814c8
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 65 deletions.
18 changes: 18 additions & 0 deletions CMake/PickyWarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ if(PICKY_COMPILER)
list(APPEND _picky "${_ccopt}")
endif()
endforeach()

if(CMAKE_COMPILER_IS_GNUCC)
if(CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5)
# Avoid false positives
list(APPEND _picky "-Wno-shadow")
list(APPEND _picky "-Wno-unreachable-code")
endif()
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.2 AND CMAKE_C_COMPILER_VERSION VERSION_LESS 4.6)
# GCC <4.6 do not support #pragma to suppress warnings locally. Disable them globally instead.
list(APPEND _picky "-Wno-overlength-strings")
endif()
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.0 AND CMAKE_C_COMPILER_VERSION VERSION_LESS 4.7)
list(APPEND _picky "-Wno-missing-field-initializers") # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36750
endif()
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3 AND CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
list(APPEND _picky "-Wno-type-limits") # Avoid false positives
endif()
endif()
endif()
endif()

Expand Down
13 changes: 4 additions & 9 deletions docs/examples/cacertinmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include <curl/curl.h>
#include <stdio.h>

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif

static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
{
fwrite(ptr, size, nmemb, (FILE *)stream);
Expand All @@ -41,11 +45,6 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
{
CURLcode rv = CURLE_ABORTED_BY_CALLBACK;

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif

/** This example uses two (fake) certificates **/
static const char mypem[] =
"-----BEGIN CERTIFICATE-----\n"
Expand Down Expand Up @@ -90,10 +89,6 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
"Z05phkOTOPu220+DkdRgfks+KzgHVZhepA==\n"
"-----END CERTIFICATE-----\n";

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif

BIO *cbio = BIO_new_mem_buf(mypem, sizeof(mypem));
X509_STORE *cts = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
int i;
Expand Down
32 changes: 13 additions & 19 deletions docs/examples/sendrecv.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
#include <string.h>
#include <curl/curl.h>

/* Avoid warning in FD_SET() with pre-2020 Cygwin/MSYS releases:
* warning: conversion to 'long unsigned int' from 'curl_socket_t' {aka 'int'}
* may change the sign of the result [-Wsign-conversion]
*/
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wsign-conversion"
#ifdef __DJGPP__
#pragma GCC diagnostic ignored "-Warith-conversion"
#endif
#elif defined(_MSC_VER)
#pragma warning(disable:4127) /* conditional expression is constant */
#endif

/* Auxiliary function that waits on the socket. */
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
{
Expand All @@ -49,20 +62,6 @@ static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
FD_ZERO(&outfd);
FD_ZERO(&errfd);

/* Avoid this warning with pre-2020 Cygwin/MSYS releases:
* warning: conversion to 'long unsigned int' from 'curl_socket_t' {aka 'int'}
* may change the sign of the result [-Wsign-conversion]
*/
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#if defined(__DJGPP__)
#pragma GCC diagnostic ignored "-Warith-conversion"
#endif
#elif defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4127) /* conditional expression is constant */
#endif
FD_SET(sockfd, &errfd); /* always check for error */

if(for_recv) {
Expand All @@ -71,11 +70,6 @@ static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
else {
FD_SET(sockfd, &outfd);
}
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif

/* select() returns the number of signalled sockets or -1 */
res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv);
Expand Down
13 changes: 4 additions & 9 deletions docs/examples/usercertinmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include <curl/curl.h>
#include <stdio.h>

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif

static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
{
fwrite(ptr, size, nmemb, stream);
Expand All @@ -55,11 +59,6 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
RSA *rsa = NULL;
int ret;

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif

const char *mypem = /* www.cacert.org */
"-----BEGIN CERTIFICATE-----\n"\
"MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"\
Expand Down Expand Up @@ -124,10 +123,6 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"\
"-----END RSA PRIVATE KEY-----\n";

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif

(void)curl; /* avoid warnings */
(void)parm; /* avoid warnings */

Expand Down
17 changes: 17 additions & 0 deletions m4/curl-compilers.m4
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,23 @@ AC_DEFUN([CURL_SET_COMPILER_WARNING_OPTS], [
fi
fi
fi
if test "$compiler_num" -lt "405"; then
dnl Avoid false positives
tmp_CFLAGS="$tmp_CFLAGS -Wno-shadow"
tmp_CFLAGS="$tmp_CFLAGS -Wno-unreachable-code"
fi
if test "$compiler_num" -ge "402" -a "$compiler_num" -lt "406"; then
dnl GCC <4.6 do not support #pragma to suppress warnings locally. Disable globally instead.
tmp_CFLAGS="$tmp_CFLAGS -Wno-overlength-strings"
fi
if test "$compiler_num" -ge "400" -a "$compiler_num" -lt "407"; then
dnl https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84685
tmp_CFLAGS="$tmp_CFLAGS -Wno-missing-field-initializers"
fi
if test "$compiler_num" -ge "403" -a "$compiler_num" -lt "408"; then
dnl Avoid false positives
tmp_CFLAGS="$tmp_CFLAGS -Wno-type-limits"
fi
;;
#
HP_UX_C)
Expand Down
20 changes: 10 additions & 10 deletions tests/http/clients/h2-pausing.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ static int debug_cb(CURL *handle, curl_infotype type,
return 0;
}

static int err(void)
{
fprintf(stderr, "something unexpected went wrong - bailing out!\n");
exit(2);
}
#define ERR() \
do { \
fprintf(stderr, "something unexpected went wrong - bailing out!\n"); \
exit(2); \
} while(0)

static void usage(const char *msg)
{
Expand Down Expand Up @@ -292,24 +292,24 @@ int main(int argc, char *argv[])
curl_easy_setopt(handles[i].h, CURLOPT_RESOLVE, resolve) != CURLE_OK ||
curl_easy_setopt(handles[i].h, CURLOPT_PIPEWAIT, 1L) ||
curl_easy_setopt(handles[i].h, CURLOPT_URL, url) != CURLE_OK) {
err();
ERR();
}
curl_easy_setopt(handles[i].h, CURLOPT_HTTP_VERSION, (long)http_version);
}

multi_handle = curl_multi_init();
if(!multi_handle)
err();
ERR();

for(i = 0; i < HANDLECOUNT; i++) {
if(curl_multi_add_handle(multi_handle, handles[i].h) != CURLM_OK)
err();
ERR();
}

for(rounds = 0;; rounds++) {
fprintf(stderr, "INFO: multi_perform round %d\n", rounds);
if(curl_multi_perform(multi_handle, &still_running) != CURLM_OK)
err();
ERR();

if(!still_running) {
int as_expected = 1;
Expand Down Expand Up @@ -343,7 +343,7 @@ int main(int argc, char *argv[])
}

if(curl_multi_poll(multi_handle, NULL, 0, 100, &numfds) != CURLM_OK)
err();
ERR();

/* !checksrc! disable EQUALSNULL 1 */
while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) {
Expand Down
12 changes: 6 additions & 6 deletions tests/http/clients/upload-pausing.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ static int progress_callback(void *clientp,
return 0;
}

static int err(void)
{
fprintf(stderr, "something unexpected went wrong - bailing out!\n");
exit(2);
}
#define ERR() \
do { \
fprintf(stderr, "something unexpected went wrong - bailing out!\n"); \
exit(2); \
} while(0)

static void usage(const char *msg)
{
Expand Down Expand Up @@ -294,7 +294,7 @@ int main(int argc, char *argv[])
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debug_cb)
!= CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_RESOLVE, resolve) != CURLE_OK)
err();
ERR();

curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, http_version);
Expand Down
4 changes: 2 additions & 2 deletions tests/libtest/lib557.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

#include "memdebug.h"

#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args"
Expand Down Expand Up @@ -1601,6 +1601,6 @@ CURLcode test(char *URL)
return CURLE_OK;
}

#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
7 changes: 7 additions & 0 deletions tests/libtest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@

#include "curl_printf.h"

/* GCC <4.6 does not support '#pragma GCC diagnostic push' and
does not support 'pragma GCC diagnostic' inside functions. */
#if (defined(__GNUC__) && \
((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))))
#define CURL_GNUC_DIAG
#endif

#ifdef _WIN32
#define sleep(sec) Sleep((sec)*1000)
#endif
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/unit1398.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
***************************************************************************/
#include "curlcheck.h"

#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#endif
Expand Down Expand Up @@ -186,6 +186,6 @@ fail_unless(rc == 128, "return code should be 128");

UNITTEST_STOP

#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
4 changes: 2 additions & 2 deletions tests/unit/unit1652.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static int verify(const char *info, const char *two)

UNITTEST_START

#if defined(__GNUC__) && !defined(__clang__)
#if defined(CURL_GNUC_DIAG) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-zero-length"
Expand Down Expand Up @@ -146,7 +146,7 @@ Curl_infof(testdata, "%s", input);
fail_unless(strlen(output) == 2051, "Truncation of infof input 3");
fail_unless(output[sizeof(output) - 1] == '\0', "Truncation of infof input 3");

#if defined(__GNUC__) && !defined(__clang__)
#if defined(CURL_GNUC_DIAG) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/unit2604.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct set {
UNITTEST_START
#ifdef USE_SSH
{
#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif
Expand Down Expand Up @@ -78,7 +78,7 @@ UNITTEST_START
{ NULL, NULL, NULL, NULL, CURLE_OK }
};

#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic warning "-Woverlength-strings"
#endif

Expand Down Expand Up @@ -114,7 +114,7 @@ UNITTEST_START

free((void *)list[0].cp);
}
#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic pop
#endif

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/unit3200.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static CURLcode unit_stop(void)
return CURLE_OK;
}

#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif
Expand Down Expand Up @@ -80,7 +80,7 @@ static const char *filecontents[] = {
"LINE1\x1aTEST"
};

#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic warning "-Woverlength-strings"
#endif

Expand Down Expand Up @@ -175,7 +175,7 @@ UNITTEST_START
return (CURLcode)rc;
UNITTEST_STOP

#if defined(__GNUC__) || defined(__clang__)
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic pop
#endif

Expand Down

0 comments on commit 7e814c8

Please sign in to comment.