Skip to content

Commit b85b25b

Browse files
authored
fix C compiler warnings (#401)
Fix C compiler warnings: - -Wcast-qual - -Wdeclaration-after-statement - -Wmissing-noreturn - -Wmissing-variable-declarations - -Wsign-conversion (some) - -Wunused-macros (most) Also: - cmake: enable warnings fixed in full. - cmake: prefer `-Wextra` in clang. (sync with curl.) - Makefile: also enable warnings fixed in full. (only those shared between clang/gcc)
1 parent 9fe363e commit b85b25b

File tree

3 files changed

+71
-54
lines changed

3 files changed

+71
-54
lines changed

CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,18 @@ endif()
8282
if(MSVC)
8383
list(APPEND _picky "-W4") # Use the highest warning level for Visual Studio.
8484
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
85-
list(APPEND _picky "-W" "-Wall" "-Wshadow" "-pedantic")
86-
list(APPEND _picky "-Wconversion" "-Wmissing-prototypes" "-Wwrite-strings" "-Wsign-compare" "-Wno-sign-conversion")
85+
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
86+
list(APPEND _picky "-Wextra")
87+
else()
88+
list(APPEND _picky "-W")
89+
endif()
90+
91+
list(APPEND _picky -Wall -pedantic)
92+
list(APPEND _picky -Wconversion -Wmissing-prototypes -Wshadow -Wsign-compare -Wno-sign-conversion -Wwrite-strings)
93+
list(APPEND _picky -Wcast-qual -Wdeclaration-after-statement -Wmissing-noreturn)
94+
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
95+
list(APPEND _picky -Wmissing-variable-declarations)
96+
endif()
8797
endif()
8898

8999
string(REPLACE ";" " " _picky_tmp "${_picky}")

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ ifndef TRURL_IGNORE_CURL_CONFIG
2828
LDLIBS += $$(curl-config --libs)
2929
CFLAGS += $$(curl-config --cflags)
3030
endif
31-
CFLAGS += -W -Wall -Wshadow -pedantic
32-
CFLAGS += -Wconversion -Wmissing-prototypes -Wwrite-strings -Wsign-compare -Wno-sign-conversion
31+
CFLAGS += -W -Wall -pedantic
32+
CFLAGS += -Wconversion -Wmissing-prototypes -Wshadow -Wsign-compare -Wno-sign-conversion -Wwrite-strings
33+
CFLAGS += -Wcast-qual -Wdeclaration-after-statement -Wmissing-noreturn
3334
ifndef NDEBUG
3435
CFLAGS += -Werror -g
3536
endif

trurl.c

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ typedef enum {
4747
#include <stdbool.h>
4848
#endif
4949

50+
/* noreturn attribute */
51+
#ifndef TRURL_NORETURN
52+
#if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) || \
53+
defined(__IAR_SYSTEMS_ICC__)
54+
# define TRURL_NORETURN __attribute__((__noreturn__))
55+
#elif defined(_MSC_VER)
56+
# define TRURL_NORETURN __declspec(noreturn)
57+
#else
58+
# define TRURL_NORETURN
59+
#endif
60+
#endif
61+
5062
#include <locale.h> /* for setlocale() */
5163

5264
#include "version.h"
@@ -89,18 +101,6 @@ typedef enum {
89101
#define CURLU_GET_EMPTY 0
90102
#endif
91103

92-
#define OUTPUT_URL 0 /* default */
93-
#define OUTPUT_SCHEME 1
94-
#define OUTPUT_USER 2
95-
#define OUTPUT_PASSWORD 3
96-
#define OUTPUT_OPTIONS 4
97-
#define OUTPUT_HOST 5
98-
#define OUTPUT_PORT 6
99-
#define OUTPUT_PATH 7
100-
#define OUTPUT_QUERY 8
101-
#define OUTPUT_FRAGMENT 9
102-
#define OUTPUT_ZONEID 10
103-
104104
#define NUM_COMPONENTS 10 /* excluding "url" */
105105

106106
#define PROGNAME "trurl"
@@ -232,7 +232,7 @@ static void warnf(const char *fmt, ...)
232232
va_end(ap);
233233
}
234234

235-
static void help(void)
235+
TRURL_NORETURN static void help(void)
236236
{
237237
int i;
238238
fputs(
@@ -265,14 +265,14 @@ static void help(void)
265265
" URL COMPONENTS:\n"
266266
" ", stdout);
267267
fputs("url, ", stdout);
268-
for(i = 0; i < NUM_COMPONENTS ; i++) {
268+
for(i = 0; i < NUM_COMPONENTS; i++) {
269269
printf("%s%s", i ? ", " : "", variables[i].name);
270270
}
271271
fputs("\n", stdout);
272272
exit(0);
273273
}
274274

275-
static void show_version(void)
275+
TRURL_NORETURN static void show_version(void)
276276
{
277277
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
278278
/* puny code isn't guaranteed based on the version, so it must be polled
@@ -382,9 +382,9 @@ static void trurl_warnf(struct option *o, const char *fmt, ...)
382382
}
383383

384384
#define MAX_QPAIRS 1000
385-
struct string qpairs[MAX_QPAIRS]; /* encoded */
386-
struct string qpairsdec[MAX_QPAIRS]; /* decoded */
387-
int nqpairs; /* how many is stored */
385+
static struct string qpairs[MAX_QPAIRS]; /* encoded */
386+
static struct string qpairsdec[MAX_QPAIRS]; /* decoded */
387+
static size_t nqpairs; /* how many is stored */
388388

389389
static void trurl_cleanup_options(struct option *o)
390390
{
@@ -405,7 +405,8 @@ static void errorf_low(const char *fmt, va_list ap)
405405
ERROR_PREFIX "Try " PROGNAME " -h for help\n", fmt, ap);
406406
}
407407

408-
static void errorf(struct option *o, int exit_code, const char *fmt, ...)
408+
TRURL_NORETURN static void errorf(struct option *o, int exit_code,
409+
const char *fmt, ...)
409410
{
410411
va_list ap;
411412
va_start(ap, fmt);
@@ -604,13 +605,13 @@ static int getarg(struct option *o,
604605
*usedarg = false;
605606

606607
if((flag[0] == '-') && (flag[1] != '-') && flag[2]) {
607-
arg = (char *)&flag[2];
608+
arg = (const char *)&flag[2];
608609
gap = false;
609610
}
610611
else if((flag[0] == '-') && (flag[1] == '-')) {
611612
char *equals = strchr(&flag[2], '=');
612613
if(equals) {
613-
arg = (char *)&equals[1];
614+
arg = (const char *)&equals[1];
614615
gap = false;
615616
}
616617
}
@@ -737,7 +738,7 @@ static int getarg(struct option *o,
737738
static void showqkey(FILE *stream, const char *key, size_t klen,
738739
bool urldecode, bool showall)
739740
{
740-
int i;
741+
size_t i;
741742
bool shown = false;
742743
struct string *qp = urldecode ? qpairsdec : qpairs;
743744

@@ -1110,10 +1111,10 @@ static unsigned int set(CURLU *uh,
11101111
static void jsonString(FILE *stream, const char *in, size_t len,
11111112
bool lowercase)
11121113
{
1113-
const unsigned char *i = (unsigned char *)in;
1114+
const unsigned char *i = (const unsigned char *)in;
11141115
const char *in_end = &in[len];
11151116
fputc('\"', stream);
1116-
for(; i < (unsigned char *)in_end; i++) {
1117+
for(; i < (const unsigned char *)in_end; i++) {
11171118
switch(*i) {
11181119
case '\\':
11191120
fputs("\\\\", stream);
@@ -1140,7 +1141,7 @@ static void jsonString(FILE *stream, const char *in, size_t len,
11401141
if(*i < 32)
11411142
fprintf(stream, "\\u%04x", *i);
11421143
else {
1143-
char out = *i;
1144+
unsigned char out = *i;
11441145
if(lowercase && (out >= 'A' && out <= 'Z'))
11451146
/* do not use tolower() since that's locale specific */
11461147
out |= ('a' - 'A');
@@ -1158,6 +1159,7 @@ static void json(struct option *o, CURLU *uh)
11581159
bool first = true;
11591160
char *url;
11601161
CURLUcode rc = geturlpart(o, 0, uh, CURLUPART_URL, &url);
1162+
bool params_errors;
11611163
if(rc) {
11621164
trurl_cleanup_options(o);
11631165
verify(o, ERROR_BADURL, "invalid url [%s]", curl_url_strerror(rc));
@@ -1168,7 +1170,7 @@ static void json(struct option *o, CURLU *uh)
11681170
curl_free(url);
11691171
fputs(",\n \"parts\": {\n", stdout);
11701172
/* special error handling required to not print params array. */
1171-
bool params_errors = false;
1173+
params_errors = false;
11721174
for(i = 0; variables[i].name; i++) {
11731175
char *part;
11741176
/* ask for the URL encoded version so that weird control characters do not
@@ -1216,9 +1218,9 @@ static void json(struct option *o, CURLU *uh)
12161218
fputs("\n }", stdout);
12171219
first = true;
12181220
if(nqpairs && !params_errors) {
1219-
int j;
1221+
size_t j;
12201222
fputs(",\n \"params\": [\n", stdout);
1221-
for(j = 0 ; j < nqpairs; j++) {
1223+
for(j = 0; j < nqpairs; j++) {
12221224
const char *sep = memchr(qpairsdec[j].str, '=', qpairsdec[j].len);
12231225
const char *value = sep ? sep + 1 : "";
12241226
int value_len = (int) qpairsdec[j].len - (int)(value - qpairsdec[j].str);
@@ -1254,7 +1256,7 @@ static bool trim(struct option *o)
12541256
asterisk */
12551257
size_t inslen;
12561258
bool pattern = false;
1257-
int i;
1259+
size_t i;
12581260
char *temp = NULL;
12591261

12601262
inslen = strlen(ptr);
@@ -1276,7 +1278,7 @@ static bool trim(struct option *o)
12761278
inslen--;
12771279
}
12781280

1279-
for(i = 0 ; i < nqpairs; i++) {
1281+
for(i = 0; i < nqpairs; i++) {
12801282
char *q = qpairs[i].str;
12811283
char *sep = strchr(q, '=');
12821284
size_t qlen;
@@ -1339,8 +1341,7 @@ static char *encodequery(char *str, size_t len)
13391341
return NULL;
13401342

13411343
while(len--) {
1342-
/* treat the characters unsigned */
1343-
unsigned char in = (unsigned char)*str++;
1344+
char in = *str++;
13441345

13451346
if(in == ' ')
13461347
*dupe++ = '+';
@@ -1350,8 +1351,8 @@ static char *encodequery(char *str, size_t len)
13501351
/* encode it */
13511352
const char hex[] = "0123456789abcdef";
13521353
dupe[0] = '%';
1353-
dupe[1] = hex[in >> 4];
1354-
dupe[2] = hex[in & 0xf];
1354+
dupe[1] = hex[(unsigned char)in >> 4];
1355+
dupe[2] = hex[(unsigned char)in & 0xf];
13551356
dupe += 3;
13561357
}
13571358
}
@@ -1376,7 +1377,7 @@ static struct string *memdupzero(char *source, size_t len, bool *modified)
13761377
char *sep = memchr(source, '=', len);
13771378
int olen;
13781379
if(!sep) { /* no '=' */
1379-
char *decode = decodequery(source, (int)len, &olen);
1380+
char *decode = decodequery(source, len, &olen);
13801381
if(decode)
13811382
encode = encodequery(decode, olen);
13821383
else
@@ -1510,7 +1511,7 @@ static struct string *memdupdec(char *source, size_t len, bool json)
15101511

15111512
static void freeqpairs(void)
15121513
{
1513-
int i;
1514+
size_t i;
15141515
for(i = 0; i < nqpairs; i++) {
15151516
if(qpairs[i].len) {
15161517
free(qpairs[i].str);
@@ -1580,7 +1581,7 @@ static bool extractqpairs(CURLU *uh, struct option *o)
15801581

15811582
static void qpair2query(CURLU *uh, struct option *o)
15821583
{
1583-
int i;
1584+
size_t i;
15841585
char *nq = NULL;
15851586
for(i = 0; i < nqpairs; i++) {
15861587
char *oldnq = nq;
@@ -1590,7 +1591,7 @@ static void qpair2query(CURLU *uh, struct option *o)
15901591
curl_free(oldnq);
15911592
}
15921593
if(nq) {
1593-
int rc = curl_url_set(uh, CURLUPART_QUERY, nq, 0);
1594+
CURLUcode rc = curl_url_set(uh, CURLUPART_QUERY, nq, 0);
15941595
if(rc)
15951596
trurl_warnf(o, "internal problem: failed to store updated query in URL");
15961597
}
@@ -1601,12 +1602,14 @@ static void qpair2query(CURLU *uh, struct option *o)
16011602
static int cmpfunc(const void *p1, const void *p2)
16021603
{
16031604
int i;
1604-
int len = (int)((((struct string *)p1)->len) < (((struct string *)p2)->len) ?
1605-
(((struct string *)p1)->len) : (((struct string *)p2)->len));
1605+
int len = (int)((((const struct string *)p1)->len) <
1606+
(((const struct string *)p2)->len) ?
1607+
(((const struct string *)p1)->len) :
1608+
(((const struct string *)p2)->len));
16061609

16071610
for(i = 0; i < len; i++) {
1608-
char c1 = ((struct string *)p1)->str[i] | ('a' - 'A');
1609-
char c2 = ((struct string *)p2)->str[i] | ('a' - 'A');
1611+
char c1 = ((const struct string *)p1)->str[i] | ('a' - 'A');
1612+
char c2 = ((const struct string *)p2)->str[i] | ('a' - 'A');
16101613
if(c1 != c2)
16111614
return c1 - c2;
16121615
}
@@ -1633,7 +1636,7 @@ static bool replace(struct option *o)
16331636
struct string key;
16341637
struct string value;
16351638
bool replaced = false;
1636-
int i;
1639+
size_t i;
16371640
key.str = node->data;
16381641
value.str = strchr(key.str, '=');
16391642
if(value.str) {
@@ -1647,6 +1650,8 @@ static bool replace(struct option *o)
16471650
}
16481651
for(i = 0; i < nqpairs; i++) {
16491652
char *q = qpairs[i].str;
1653+
struct string *pdec, *p;
1654+
16501655
/* not the correct query, move on */
16511656
if(strncmp(q, key.str, key.len))
16521657
continue;
@@ -1660,11 +1665,10 @@ static bool replace(struct option *o)
16601665
qpairsdec[i].str = xstrdup(o, "");
16611666
continue;
16621667
}
1663-
struct string *pdec =
1664-
memdupdec(key.str, key.len + value.len + 1, o->jsonout);
1665-
struct string *p = memdupzero(key.str, key.len + value.len +
1666-
(value.str ? 1 : 0),
1667-
&query_is_modified);
1668+
pdec = memdupdec(key.str, key.len + value.len + 1, o->jsonout);
1669+
p = memdupzero(key.str, key.len + value.len +
1670+
(value.str ? 1 : 0),
1671+
&query_is_modified);
16681672
qpairs[i].len = p->len;
16691673
qpairs[i].str = p->str;
16701674
qpairsdec[i].len = pdec->len;
@@ -1705,8 +1709,10 @@ static char *canonical_path(const char *path)
17051709
char *npath;
17061710
char *ndupe;
17071711
int olen;
1712+
size_t partlen;
1713+
17081714
sl = memchr(path, '/', len);
1709-
size_t partlen = sl ? (size_t)(sl - path) : len;
1715+
partlen = sl ? (size_t)(sl - path) : len;
17101716

17111717
if(partlen) {
17121718
/* First URL decode the part */
@@ -2011,7 +2017,7 @@ static void singleurl(struct option *o,
20112017
else {
20122018
/* default output is full URL */
20132019
char *nurl = NULL;
2014-
int rc = geturlpart(o, 0, uh, CURLUPART_URL, &nurl);
2020+
CURLUcode rc = geturlpart(o, 0, uh, CURLUPART_URL, &nurl);
20152021
if(!rc) {
20162022
printf("%s\n", nurl);
20172023
curl_free(nurl);

0 commit comments

Comments
 (0)