Skip to content

Commit 9c45339

Browse files
committed
Fix compilation errors for GCC 4.2.1
Compilation warnings and errors kindly reported by Florian Obser.
1 parent cba9264 commit 9c45339

File tree

13 files changed

+107
-113
lines changed

13 files changed

+107
-113
lines changed

include/zone.h

+36-49
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <stdint.h>
1919
#include <stdbool.h>
2020
#include <stdio.h>
21-
#include <stdlib.h>
21+
#include <stddef.h>
2222

2323
#include "zone/attributes.h"
2424
#include "zone/export.h"
@@ -444,57 +444,44 @@ zone_format_printf(3,4);
444444
/**
445445
* @brief Write error message to active log handler.
446446
*
447-
* @param[in] parser Zone parser
448-
* @param[in] priority Log priority
449-
* @param[in] format Format string compatible with printf
450-
* @param[in] arguments Argument list
447+
* Shorthand to write out error message via @zone_log if error messages are
448+
* not to be discarded.
449+
*
450+
* @param[in] parser Zone parser
451+
* @param[in] format Format string
452+
* @param[in] ... Variadic arguments corresponding to #format
451453
*/
452-
ZONE_EXPORT void zone_vlog(
453-
zone_parser_t *parser,
454-
uint32_t priority,
455-
const char *format,
456-
va_list arguments)
457-
zone_nonnull((1,3));
454+
#define zone_error(parser, ...) \
455+
(((parser)->options.log.mask & ZONE_ERROR) ? \
456+
(void)0 : zone_log((parser), ZONE_ERROR, __VA_ARGS__))
458457

459-
ZONE_EXPORT inline void
460-
zone_nonnull((1,2))
461-
zone_format_printf(2,3)
462-
zone_error(zone_parser_t *parser, const char *format, ...)
463-
{
464-
va_list arguments;
465-
if (!(ZONE_ERROR & ~parser->options.log.mask))
466-
return;
467-
va_start(arguments, format);
468-
zone_vlog(parser, ZONE_ERROR, format, arguments);
469-
va_end(arguments);
470-
}
471-
472-
ZONE_EXPORT inline void
473-
zone_nonnull((1,2))
474-
zone_format_printf(2,3)
475-
zone_warning(zone_parser_t *parser, const char *format, ...)
476-
{
477-
va_list arguments;
478-
if (!(ZONE_WARNING & ~parser->options.log.mask))
479-
return;
480-
va_start(arguments, format);
481-
zone_vlog(parser, ZONE_WARNING, format, arguments);
482-
va_end(arguments);
483-
}
484-
485-
ZONE_EXPORT inline void
486-
zone_nonnull((1,2))
487-
zone_format_printf(2,3)
488-
zone_info(zone_parser_t *parser, const char *format, ...)
489-
{
490-
va_list arguments;
491-
if (!(ZONE_INFO & ~parser->options.log.mask))
492-
return;
493-
va_start(arguments, format);
494-
zone_vlog(parser, ZONE_INFO, format, arguments);
495-
va_end(arguments);
496-
}
458+
/**
459+
* @brief Write warning message to active log handler.
460+
*
461+
* Shorthand to write out warning message via @zone_log if warning messages
462+
* are not to be discarded.
463+
*
464+
* @param[in] parser Zone parser
465+
* @param[in] format Format string compatible with printf.
466+
* @param[in] ... Variadic arguments corresponding to @format.
467+
*/
468+
#define zone_warning(parser, ...) \
469+
(((parser)->options.mask & ZONE_WARNING) ? \
470+
(void)0 : zone_log((parser), ZONE_WARNING, __VA_ARGS__))
497471

472+
/**
473+
* @brief Write informational message to active log handler.
474+
*
475+
* Shorthand to write out informational message via @zone_log if
476+
* informational messages are not be discarded.
477+
*
478+
* @param[in] parser Zone parser.
479+
* @param[in] format Format string compatible with printf.
480+
* @param[in] ... Variadic arguments corresponding to @format.
481+
*/
482+
#define zone_info(parser, ...) \
483+
(((parser)->options.mask & ZONE_INFO) ? \
484+
(void)0 : zone_log((parser), ZONE_INFO, __VA_ARGS__))
498485

499486
#if defined(__cplusplus)
500487
}

include/zone/attributes.h

+22-30
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,42 @@
99
#ifndef ZONE_ATTRIBUTES_H
1010
#define ZONE_ATTRIBUTES_H
1111

12-
#if __clang__
13-
# define zone_clang \
14-
(__clang_major__ * 100000 + __clang_minor__ * 100 + __clang_patchlevel__)
15-
#elif __GCC__
16-
# define zone_gcc \
17-
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
12+
#if defined __GNUC__
13+
# define zone_has_gnuc(major, minor) \
14+
((__GNUC__ > major) || (__GNUC__ == major && __GNUC_MINOR__ >= minor))
1815
#endif
1916

2017
#if defined __has_attribute
2118
# define zone_has_attribute(params) __has_attribute(params)
22-
# define zone_attribute(params) __attribute__(params)
23-
#elif zone_gcc
24-
# define zone_has_attribute(params) __has_attribute(params)
25-
# define zone_attribute(params) __attribute__(params)
2619
#else
2720
# define zone_has_attribute(params) (0)
28-
# define zone_attribute(params)
2921
#endif
3022

31-
#define zone_nonnull(params) zone_attribute((__nonnull__ params))
32-
#define zone_nonnull_all zone_attribute((__nonnull__))
23+
#if zone_has_attribute(nonnull)
24+
# define zone_nonnull(params) __attribute__((__nonnull__ params))
25+
# define zone_nonnull_all __attribute__((__nonnull__))
26+
#else
27+
# define zone_nonnull(params)
28+
# define zone_nonnull_all
29+
#endif
3330

34-
#if _MSC_VER
35-
# define zone_format(params)
36-
# define zone_format_printf(string_index, first_to_check)
37-
#else // _MSC_VER
38-
# if zone_has_attribute(format)
39-
# define zone_format(params) zone_attribute((__format__ params))
40-
# if __MINGW32__
41-
# if __MINGW_PRINTF_FORMAT
42-
# define zone_format_printf(string_index, first_to_check) \
43-
zone_format((__MINGW_PRINTF_FORMAT, string_index, first_to_check))
44-
# else
45-
# define zone_format_printf(string_index, first_to_check) \
46-
zone_format((gnu_printf, string_index, first_to_check))
47-
# endif
31+
#if zone_has_attribute(format) || zone_has_gnuc(2, 4)
32+
# define zone_format(params) __attribute__((__format__ params))
33+
# if __MINGW32__
34+
# if __MINGW_PRINTF_FORMAT
35+
# define zone_format_printf(string_index, first_to_check) \
36+
zone_format((__MINGW_PRINTF_FORMAT, string_index, first_to_check))
4837
# else
4938
# define zone_format_printf(string_index, first_to_check) \
50-
zone_format((printf, string_index, first_to_check))
39+
zone_format((gnu_printf, string_index, first_to_check))
5140
# endif
5241
# else
53-
# define zone_format(params)
54-
# define zone_format_printf(string_index, first_to_check)
42+
# define zone_format_printf(string_index, first_to_check) \
43+
zone_format((printf, string_index, first_to_check))
5544
# endif
45+
#else
46+
# define zone_format(params)
47+
# define zone_format_printf(string_index, first_to_check)
5648
#endif
5749

5850
#endif // ZONE_ATTRIBUTES_H

src/attributes.h

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* attributes.h -- internal compiler attribute abstractions
33
*
4-
* Copyright (c) 2023, NLnet Labs. All rights reserved.
4+
* Copyright (c) 2023-2024, NLnet Labs. All rights reserved.
55
*
66
* SPDX-License-Identifier: BSD-3-Clause
77
*
@@ -22,13 +22,21 @@
2222
# define likely(params) (params)
2323
# define unlikely(params) (params)
2424

25-
# define zone_format(params)
26-
# define zone_format_printf(string_index, first_to_check)
2725
#else // _MSC_VER
28-
# define really_inline inline zone_attribute((always_inline))
29-
# define never_inline zone_attribute((noinline))
26+
# if zone_has_attribute(always_inline) || zone_has_gnuc(3, 1)
27+
# define really_inline inline __attribute__((always_inline))
28+
# else
29+
# define really_inline inline
30+
# endif
31+
32+
# if zone_has_attribute(noinline) || zone_has_gnuc(2, 96)
33+
# define never_inline __attribute__((noinline))
34+
# else
35+
# define never_inline
36+
# endif
37+
3038
# if zone_has_attribute(warn_unused_result)
31-
# define warn_unused_result zone_attribute((warn_unused_result))
39+
# define warn_unused_result __attribute__((warn_unused_result))
3240
# else
3341
# define warn_unused_result
3442
# endif

src/bench.c

+11-7
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,21 @@ int main(int argc, char *argv[])
211211
exit(EXIT_FAILURE);
212212

213213
zone_parser_t parser = { 0 };
214-
zone_options_t options = { 0 };
214+
zone_options_t options = {
215+
.non_strict = 0,
216+
.no_includes = false,
217+
.include_limit = 0,
218+
.pretty_ttls = true,
219+
.origin = { .octets = root, .length = 1 },
220+
.default_ttl = 3600,
221+
.default_class = 1,
222+
.log = { .mask = 0, .callback = 0 },
223+
.accept = { .callback = &bench_accept }
224+
};
215225
zone_name_buffer_t owner;
216226
zone_rdata_buffer_t rdata;
217227
zone_buffers_t buffers = { 1, &owner, &rdata };
218228

219-
options.accept.callback = &bench_accept;
220-
options.origin.octets = root;
221-
options.origin.length = 1;
222-
options.default_ttl = 3600;
223-
options.default_class = ZONE_IN;
224-
225229
if (zone_open(&parser, &options, &buffers, argv[argc-1], NULL) < 0)
226230
exit(EXIT_FAILURE);
227231
if (bench(&parser, kernel) < 0)

src/diagnostic.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
__pragma(warning(disable:warning_specifier))
1717
# define diagnostic_pop() \
1818
__pragma(warning(pop))
19-
#elif __GNUC__
19+
// Support for selectively enabling and disabling warnings via
20+
// #pragma GCC diagnostic was added in GCC 4.6
21+
// (https://gcc.gnu.org/gcc-4.6/changes.html).
22+
#elif (defined __clang__) \
23+
|| (defined __GNUC__ && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 406))
2024
# define stringify(x) #x
2125
# define paste(flag, warning) stringify(flag ## warning)
2226
# define pragma(x) _Pragma(#x)
@@ -27,10 +31,10 @@
2731
# if __clang__
2832
# define clang_diagnostic_ignored(warning) \
2933
diagnostic_ignored(GCC diagnostic ignored paste(-W,warning))
30-
# elif __GNUC__ && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
34+
# else
3135
# define gcc_diagnostic_ignored(warning) \
3236
diagnostic_ignored(GCC diagnostic ignored paste(-W,warning))
33-
#endif
37+
# endif
3438
#endif
3539

3640
#if !defined diagnostic_push

src/generic/base16.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static really_inline int32_t parse_base16_sequence(
236236
token_t *token)
237237
{
238238
if (is_contiguous(token)) {
239-
struct base16_state state = { 0 };
239+
struct base16_state state = { .eof = 0, .bytes = 0, .carry = 0 };
240240

241241
do {
242242
size_t length = (token->length + 1) / 2;

src/generic/base64.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ static really_inline int base64_decode(
679679
uint8_t *out,
680680
size_t *outlen)
681681
{
682-
struct base64_state state = { 0 };
682+
struct base64_state state = { .eof = 0, .bytes = 0, .carry = 0 };
683683
return base64_stream_decode(&state, src, srclen, out, outlen) & !state.bytes;
684684
}
685685

@@ -692,7 +692,7 @@ static really_inline int32_t parse_base64_sequence(
692692
token_t *token)
693693
{
694694
if (is_contiguous(token)) {
695-
struct base64_state state = { 0 };
695+
struct base64_state state = { .eof = 0, .bytes = 0, .carry = 0 };
696696

697697
do {
698698
size_t length = token->length / 4;

src/generic/parser.h

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <stdbool.h>
1414
#include <stdint.h>
1515
#include <string.h>
16+
#include <stdlib.h>
1617

1718
#if _MSC_VER
1819
# define strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))

src/generic/svcb.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static int32_t parse_ech(
242242
if (token->length / 4 > size / 3)
243243
SYNTAX_ERROR(parser, "maximum size exceeded");
244244

245-
struct base64_state state = { 0 };
245+
struct base64_state state = { .eof = 0, .bytes = 0, .carry = 0 };
246246
if (!base64_stream_decode(
247247
&state, token->data, token->length, rdata->octets, &length))
248248
SYNTAX_ERROR(parser, "Invalid ech in %s", NAME(type));

src/generic/types.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@ static int32_t parse_generic_rdata(
23942394

23952395
take(parser, token);
23962396
if (is_contiguous(token)) {
2397-
struct base16_state state = { 0 };
2397+
struct base16_state state = { .eof = 0, .bytes = 0, .carry = 0 };
23982398

23992399
do {
24002400
size_t length = token->length + 1 / 2;

src/zone.c

+9-13
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ static void print_message(
473473
fprintf(output, format, parser->file->name, parser->file->line, message);
474474
}
475475

476+
void zone_vlog(
477+
zone_parser_t *parser,
478+
uint32_t priority,
479+
const char *format,
480+
va_list arguments);
481+
476482
void zone_vlog(
477483
zone_parser_t *parser,
478484
uint32_t priority,
@@ -483,6 +489,9 @@ void zone_vlog(
483489
int length;
484490
zone_log_t callback = print_message;
485491

492+
if (!(priority & ~parser->options.log.mask))
493+
return;
494+
486495
length = vsnprintf(message, sizeof(message), format, arguments);
487496
assert(length >= 0);
488497
if ((size_t)length >= sizeof(message))
@@ -500,20 +509,7 @@ void zone_log(
500509
...)
501510
{
502511
va_list arguments;
503-
504-
if (!(priority & ~parser->options.log.mask))
505-
return;
506-
507512
va_start(arguments, format);
508513
zone_vlog(parser, priority, format, arguments);
509514
va_end(arguments);
510515
}
511-
512-
ZONE_EXPORT extern inline void
513-
zone_error(zone_parser_t *parser, const char *format, ...);
514-
515-
ZONE_EXPORT extern inline void
516-
zone_warning(zone_parser_t *parser, const char *format, ...);
517-
518-
ZONE_EXPORT extern inline void
519-
zone_info(zone_parser_t *parser, const char *format, ...);

tests/syntax.c

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdarg.h>
1212
#include <setjmp.h>
1313
#include <string.h>
14+
#include <stdlib.h>
1415
#include <cmocka.h>
1516
#if !_WIN32
1617
#include <unistd.h>

tests/time.c

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdarg.h>
1010
#include <setjmp.h>
1111
#include <string.h>
12+
#include <stdlib.h>
1213
#include <cmocka.h>
1314

1415
#include "zone.h"

0 commit comments

Comments
 (0)