]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
GCC12: Fixes for 16+
authorGeorge Joseph <gjoseph@digium.com>
Tue, 3 May 2022 12:57:58 +0000 (06:57 -0600)
committerFriendly Automation <jenkins2@gerrit.asterisk.org>
Mon, 9 May 2022 12:49:52 +0000 (07:49 -0500)
Most issues were in stringfields and had to do with comparing
a pointer to an constant/interned string with NULL.  Since the
string was a constant, a pointer to it could never be NULL so
the comparison was always "true".  gcc now complains about that.

There were also a few issues where determining if there was
enough space for a memcpy or s(n)printf which were fixed
by defining some of the involved variables as "volatile".

There were also a few other miscellaneous fixes.

ASTERISK-30044

Change-Id: Ia081ca1bcfb329df6487c4660aaf1944309eb570

12 files changed:
addons/Makefile
apps/app_festival.c
channels/chan_sip.c
channels/sig_analog.c
funcs/func_scramble.c
include/asterisk/stringfields.h
include/asterisk/strings.h
main/pbx.c
main/stun.c
res/res_config_pgsql.c
res/res_tonedetect.c
tests/test_vector.c

index ae97ffb1c281d6b974bdf2b0212c91e0bc3056e8..06360dbfa54748b7138985769df41ed2a785fa6c 100644 (file)
@@ -62,6 +62,10 @@ chan_ooh323.so: _ASTCFLAGS+=$(H323CFLAGS)
 $(call MOD_ADD_C,chan_ooh323,$(H323SOURCE))
 
 ifneq ($(wildcard mp3/Makefile),)
+# At the current time, the fate of mp3 is in flux so it didn't make sense to
+# add configure/makeopts processing for array-bounds since this is the only
+# source file that needs that warning suppressed.
+mp3/layer3.o: _ASTCFLAGS+=-Wno-array-bounds
 $(call MOD_ADD_C,format_mp3,mp3/common.c mp3/dct64_i386.c mp3/decode_ntom.c mp3/layer3.c mp3/tabinit.c mp3/interface.c)
 
 .PHONY: check_mp3
index c18725dbb6caa6888f1b2fee0cc945a21bb27638..5348bc79da25ee8dcfdd1f4a4aad4ca850902d97 100644 (file)
@@ -433,7 +433,7 @@ static int festival_exec(struct ast_channel *chan, const char *vdata)
        }
        readcache = 0;
        writecache = 0;
-       if (strlen(cachedir) + strlen(MD5Hex) + 1 <= MAXFESTLEN && (usecache == -1)) {
+       if (strlen(cachedir) + sizeof(MD5Hex) + 1 <= MAXFESTLEN && (usecache == -1)) {
                snprintf(cachefile, sizeof(cachefile), "%s/%s", cachedir, MD5Hex);
                fdesc = open(cachefile, O_RDWR);
                if (fdesc == -1) {
index affd73fbd561afaa07ae192501f4f8e93004dad1..bf03c5ff268f9e1534b281c7a5ebca8e3db29922 100644 (file)
@@ -35420,8 +35420,8 @@ AST_TEST_DEFINE(get_in_brackets_const_test)
                        ast_test_status_update(test, "Unexpected result: %d != %d\n", expected_res, res); \
                        return AST_TEST_FAIL;                           \
                }                                                       \
-               if ((expected_start) != start) {                        \
-                       const char *e = expected_start ? expected_start : "(null)"; \
+               if ((void *)(expected_start) != (void *)start) {                        \
+                       const char *e = ((void *)expected_start != (void *)NULL) ? expected_start : "(null)"; \
                        const char *a = start ? start : "(null)";       \
                        ast_test_status_update(test, "Unexpected start: %s != %s\n", e, a); \
                        return AST_TEST_FAIL;                           \
index 64f1114fde9bcd99a2e7e815b32bad1dd8bd1f77..ea507fe078f45ce74480c9966226c73d873fcfed 100644 (file)
@@ -1033,6 +1033,10 @@ int analog_call(struct analog_pvt *p, struct ast_channel *ast, const char *rdest
                                ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
                                c = NULL;
                        }
+                       if (c && (strlen(c) > sizeof(p->dop.dialstr) - 3 /* "Tw\0" */)) {
+                               ast_log(LOG_WARNING, "Number '%s' is longer than %d bytes\n", c, (int)sizeof(p->dop.dialstr) - 2);
+                               c = NULL;
+                       }
                        if (c) {
                                p->dop.op = ANALOG_DIAL_OP_REPLACE;
                                snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
index a90d7a277403552825ab0bc8e53bfd7142fa704d..5fe194cd15f3dbe4f767966a130e0b8fb90ca7c1 100644 (file)
@@ -126,7 +126,7 @@ static int scramble_callback(struct ast_audiohook *audiohook, struct ast_channel
 
        if (frame->frametype == AST_FRAME_VOICE) { /* only invert voice frequencies */
                /* Based on direction of frame, and confirm it is applicable */
-               if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? &ni->rx : &ni->tx)) {
+               if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? ni->rx : ni->tx)) {
                        return 0;
                }
                /* Scramble the sample now */
index edf479d75b4468471e8a6bf4d6e90dd201f2bf42..94ba756e98ad9d533dffe6dcfb22b68be72689d0 100644 (file)
@@ -355,10 +355,11 @@ enum ast_stringfield_cleanup_type {
   \retval zero on success
   \retval non-zero on failure
 */
+
 #define ast_string_field_init(x, size) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                __res__ = __ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, size, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
        } \
        __res__ ; \
@@ -373,7 +374,7 @@ enum ast_stringfield_cleanup_type {
 #define ast_string_field_free_memory(x)         \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                __res__ = __ast_string_field_free_memory(&(x)->__field_mgr, &(x)->__field_mgr_pool, \
                        AST_STRINGFIELD_DESTROY, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
        } \
@@ -400,7 +401,7 @@ int __ast_string_field_free_memory(struct ast_string_field_mgr *mgr,
 #define ast_string_field_init_extended(x, field) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                ast_string_field *non_const = (ast_string_field *)&(x)->field; \
                *non_const = __ast_string_field_empty; \
                __res__ = AST_VECTOR_APPEND(&(x)->__field_mgr.string_fields, non_const); \
@@ -474,7 +475,7 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
 #define ast_string_field_ptr_set(x, ptr, data) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                __res__ = ast_string_field_ptr_set_by_fields((x)->__field_mgr_pool, (x)->__field_mgr, ptr, data); \
        } \
        __res__; \
@@ -482,26 +483,28 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
 
 #define __ast_string_field_ptr_set_by_fields(field_mgr_pool, field_mgr, ptr, data, file, lineno, func) \
 ({                                                                                             \
-    int __res__ = 0;                                                                           \
-    const char *__d__ = (data);                                                                \
-    size_t __dlen__ = (__d__) ? strlen(__d__) + 1 : 1;                                         \
-    ast_string_field *__p__ = (ast_string_field *) (ptr);                                      \
-    ast_string_field target = *__p__;                                                          \
-    if (__dlen__ == 1) {                                                                       \
-        __ast_string_field_release_active(field_mgr_pool, *__p__);                             \
-        *__p__ = __ast_string_field_empty;                                                     \
-    } else if ((__dlen__ <= AST_STRING_FIELD_ALLOCATION(*__p__)) ||                            \
-           (!__ast_string_field_ptr_grow(&field_mgr, &field_mgr_pool, __dlen__, __p__)) ||     \
-           (target = __ast_string_field_alloc_space(&field_mgr, &field_mgr_pool, __dlen__, file, lineno, func))) { \
-        if (target != *__p__) {                                                                \
-            __ast_string_field_release_active(field_mgr_pool, *__p__);                         \
-            *__p__ = target;                                                                   \
-        }                                                                                      \
-        memcpy(* (void **) __p__, __d__, __dlen__);                                            \
-    } else {                                                                                   \
-        __res__ = -1;                                                                          \
-    }                                                                                          \
-    __res__;                                                                                   \
+       int __res__ = 0;                                                                           \
+       const char *__d__ = (data);                                                                \
+       ast_string_field *__p__ = (ast_string_field *) (ptr);                                      \
+       ast_string_field target = *__p__;                                                          \
+       if (__d__ == NULL || *__d__ == '\0') {                                                     \
+               __ast_string_field_release_active(field_mgr_pool, *__p__);                             \
+               *__p__ = __ast_string_field_empty;                                                     \
+       } else {                                                                                   \
+               size_t __dlen__ = strlen(__d__) + 1;                                                   \
+               if ((__dlen__ <= AST_STRING_FIELD_ALLOCATION(*__p__)) ||                               \
+                       (!__ast_string_field_ptr_grow(&field_mgr, &field_mgr_pool, __dlen__, __p__)) ||    \
+                       (target = __ast_string_field_alloc_space(&field_mgr, &field_mgr_pool, __dlen__, file, lineno, func))) { \
+                       if (target != *__p__) {                                                            \
+                               __ast_string_field_release_active(field_mgr_pool, *__p__);                     \
+                               *__p__ = target;                                                               \
+                       }                                                                                  \
+                       memcpy(* (void **) __p__, __d__, __dlen__);                                        \
+               } else {                                                                               \
+                       __res__ = -1;                                                                      \
+               }                                                                                      \
+       }                                                                                          \
+       __res__;                                                                                   \
 })
 
 #define ast_string_field_ptr_set_by_fields(field_mgr_pool, field_mgr, ptr, data) \
@@ -518,7 +521,7 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
 #define ast_string_field_set(x, field, data) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                __res__ = ast_string_field_ptr_set(x, &(x)->field, data); \
        } \
        __res__; \
@@ -534,7 +537,7 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
 #define ast_string_field_ptr_build(x, ptr, fmt, args...) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                __ast_string_field_ptr_build(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
                        &(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) ptr, fmt, args); \
                __res__ = 0; \
@@ -552,7 +555,7 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
 #define ast_string_field_build(x, field, fmt, args...) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                __ast_string_field_ptr_build(__FILE__, __LINE__, __PRETTY_FUNCTION__, \
                        &(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) &(x)->field, fmt, args); \
                __res__ = 0; \
@@ -570,7 +573,7 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
 #define ast_string_field_ptr_build_va(x, ptr, fmt, args) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                __ast_string_field_ptr_build_va(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) ptr, fmt, args, \
                        __FILE__, __LINE__, __PRETTY_FUNCTION__); \
                __res__ = 0; \
@@ -588,7 +591,7 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
 #define ast_string_field_build_va(x, field, fmt, args) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(x)) != NULL) { \
+       if (((void *)(x)) != (void *)NULL) { \
                __ast_string_field_ptr_build_va(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) &(x)->field, fmt, args, \
                        __FILE__, __LINE__, __PRETTY_FUNCTION__); \
                __res__ = 0; \
@@ -607,7 +610,7 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
 #define ast_string_fields_cmp(instance1, instance2) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(instance1)) != NULL && ((void *)(instance2)) != NULL) { \
+       if (((void *)(instance1)) != (void *)NULL && ((void *)(instance2)) != (void *)NULL) { \
                __res__ = __ast_string_fields_cmp(&(instance1)->__field_mgr.string_fields, \
                        &(instance2)->__field_mgr.string_fields); \
        } \
@@ -627,7 +630,7 @@ int __ast_string_fields_cmp(struct ast_string_field_vector *left, struct ast_str
 #define ast_string_fields_copy(copy, orig) \
 ({ \
        int __res__ = -1; \
-       if (((void *)(copy)) != NULL && ((void *)(orig)) != NULL) { \
+       if (((void *)(copy)) != (void *)NULL && ((void *)(orig)) != (void *)NULL) { \
                __res__ = __ast_string_fields_copy(((copy)->__field_mgr_pool), \
                        (struct ast_string_field_mgr *)&((copy)->__field_mgr), \
                        (struct ast_string_field_mgr *)&((orig)->__field_mgr), \
index ab46273b60f86ce7caac3404d98a6c7baf15d452..93983c3e2bba73d22170feedbb0f0c50d661facd 100644 (file)
@@ -393,11 +393,13 @@ char *ast_escape_c_alloc(const char *s);
 AST_INLINE_API(
 void ast_copy_string(char *dst, const char *src, size_t size),
 {
-       while (*src && size) {
-               *dst++ = *src++;
-               size--;
+       volatile size_t sz = size;
+       volatile char *sp = (char *)src;
+       while (*sp && sz) {
+               *dst++ = *sp++;
+               sz--;
        }
-       if (__builtin_expect(!size, 0))
+       if (__builtin_expect(!sz, 0))
                dst--;
        *dst = '\0';
 }
index 1e08f233dc05ec60b0d689b7f4ecb0f12074a101..3a701aa0581937aa5551d4d2086c07f24ed2246d 100644 (file)
@@ -1652,6 +1652,7 @@ static const char *get_pattern_node(struct pattern_node *node, const char *src,
 #undef INC_DST_OVERFLOW_CHECK
 }
 
+#define MAX_EXTENBUF_SIZE 512
 static struct match_char *add_exten_to_pattern_tree(struct ast_context *con, struct ast_exten *e1, int findonly)
 {
        struct match_char *m1 = NULL;
@@ -1662,11 +1663,13 @@ static struct match_char *add_exten_to_pattern_tree(struct ast_context *con, str
        int pattern = 0;
        int idx_cur;
        int idx_next;
-       char extenbuf[512];
+       char extenbuf[MAX_EXTENBUF_SIZE];
+       volatile size_t required_space = strlen(e1->exten) + 1;
        struct pattern_node pat_node[2];
 
        if (e1->matchcid) {
-               if (sizeof(extenbuf) < strlen(e1->exten) + strlen(e1->cidmatch) + 2) {
+               required_space += (strlen(e1->cidmatch) + 2 /* '/' + NULL */);
+               if (required_space > MAX_EXTENBUF_SIZE) {
                        ast_log(LOG_ERROR,
                                "The pattern %s/%s is too big to deal with: it will be ignored! Disaster!\n",
                                e1->exten, e1->cidmatch);
@@ -1674,7 +1677,13 @@ static struct match_char *add_exten_to_pattern_tree(struct ast_context *con, str
                }
                sprintf(extenbuf, "%s/%s", e1->exten, e1->cidmatch);/* Safe.  We just checked. */
        } else {
-               ast_copy_string(extenbuf, e1->exten, sizeof(extenbuf));
+               if (required_space > MAX_EXTENBUF_SIZE) {
+                       ast_log(LOG_ERROR,
+                               "The pattern %s/%s is too big to deal with: it will be ignored! Disaster!\n",
+                               e1->exten, e1->cidmatch);
+                       return NULL;
+               }
+               ast_copy_string(extenbuf, e1->exten, required_space);
        }
 
 #ifdef NEED_DEBUG
index 8007f3a3ed647c4b23b2f99ae313ab8354302e2e..06e6d9ff2997026c53d1d8729ea528cb757cd508 100644 (file)
@@ -370,7 +370,13 @@ int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data,
                                            st.username ? st.username : "<none>");
                        if (st.username) {
                                append_attr_string(&attr, STUN_USERNAME, st.username, &resplen, &respleft);
-                               snprintf(combined, sizeof(combined), "%16s%16s", st.username + 16, st.username);
+                               /*
+                                * For Google Voice, the stun username is made up of the local
+                                * and remote usernames, each being fixed at 16 bytes.  We have
+                                * to swap the two at this point.
+                                */
+                               snprintf(combined, 17, "%16s", st.username + 16);
+                               snprintf(combined + 16, 17, "%16s", st.username);
                        } else {
                                combined[0] = '\0';
                        }
index dbcd9e77b6507cb06d5c6becae466a7934e9b48e..62a3565e024ef4902b66833044678aa5e17ec390 100644 (file)
@@ -1287,7 +1287,7 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                                res = -1;
                        } else {
                                struct ast_str *sql = ast_str_create(100);
-                               char fieldtype[10];
+                               char fieldtype[20];
                                PGresult *result;
 
                                if (requirements == RQ_CREATECHAR || type == RQ_CHAR) {
index 45afe5b084dfe00172e4261719ba9696d3705ef1..055142bbef32b906d344a5ea538f6340deda5cc5 100644 (file)
@@ -367,7 +367,7 @@ static int detect_callback(struct ast_audiohook *audiohook, struct ast_channel *
                return 0;
        }
 
-       if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? &di->rx : &di->tx)) {
+       if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? di->rx : di->tx)) {
                return 0;
        }
 
index 2dfcc60a8c61018b5ce89b3e64224f903f8431c0..5f6f0a54b395b0c52716d05047d0aa9558a9f937 100644 (file)
@@ -48,7 +48,7 @@ static void cleanup(char *element)
 }
 
 #define STRING_CMP(a, b) ({ \
-       ((a) == NULL || (b) == NULL) ? -1 : (strcmp((a), (b)) == 0); \
+       ((void *)(a) == (void *)NULL || (void *)(b) == (void *)NULL) ? -1 : (strcmp((a), (b)) == 0); \
 })
 
 AST_TEST_DEFINE(basic_ops)