[Asterisk-code-review] GCC12: Fixes for 16+ (asterisk[16])

Friendly Automation asteriskteam at digium.com
Mon May 9 07:52:03 CDT 2022


Friendly Automation has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/18529 )

Change subject: GCC12: Fixes for 16+
......................................................................

GCC12: Fixes for 16+

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
---
M addons/Makefile
M apps/app_festival.c
M channels/chan_sip.c
M channels/sig_analog.c
M funcs/func_scramble.c
M include/asterisk/stringfields.h
M include/asterisk/strings.h
M main/pbx.c
M main/stun.c
M res/res_config_pgsql.c
M res/res_tonedetect.c
M tests/test_vector.c
12 files changed, 74 insertions(+), 46 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Michael Bradeen: Looks good to me, but someone else must approve
  Friendly Automation: Approved for Submit



diff --git a/addons/Makefile b/addons/Makefile
index 866d34b..c41ffdc 100644
--- a/addons/Makefile
+++ b/addons/Makefile
@@ -64,6 +64,10 @@
 $(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
diff --git a/apps/app_festival.c b/apps/app_festival.c
index c18725d..5348bc7 100644
--- a/apps/app_festival.c
+++ b/apps/app_festival.c
@@ -433,7 +433,7 @@
 	}
 	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) {
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 565f9e1..a43908c 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -35422,8 +35422,8 @@
 			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;				\
diff --git a/channels/sig_analog.c b/channels/sig_analog.c
index 64f1114..ea507fe 100644
--- a/channels/sig_analog.c
+++ b/channels/sig_analog.c
@@ -1033,6 +1033,10 @@
 				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);
diff --git a/funcs/func_scramble.c b/funcs/func_scramble.c
index a90d7a2..5fe194c 100644
--- a/funcs/func_scramble.c
+++ b/funcs/func_scramble.c
@@ -126,7 +126,7 @@
 
 	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 */
diff --git a/include/asterisk/stringfields.h b/include/asterisk/stringfields.h
index edf479d..94ba756 100644
--- a/include/asterisk/stringfields.h
+++ b/include/asterisk/stringfields.h
@@ -355,10 +355,11 @@
   \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 @@
 #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 @@
 #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 @@
 #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 @@
 
 #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 @@
 #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 @@
 #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 @@
 #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 @@
 #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 @@
 #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 @@
 #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 @@
 #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), \
diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index ab46273..93983c3 100644
--- a/include/asterisk/strings.h
+++ b/include/asterisk/strings.h
@@ -393,11 +393,13 @@
 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';
 }
diff --git a/main/pbx.c b/main/pbx.c
index 1e08f23..3a701aa 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -1652,6 +1652,7 @@
 #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 @@
 	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 @@
 		}
 		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
diff --git a/main/stun.c b/main/stun.c
index 8007f3a..06e6d9f 100644
--- a/main/stun.c
+++ b/main/stun.c
@@ -370,7 +370,13 @@
 					    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';
 			}
diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c
index dbcd9e7..62a3565 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -1287,7 +1287,7 @@
 				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) {
diff --git a/res/res_tonedetect.c b/res/res_tonedetect.c
index 45afe5b..055142b 100644
--- a/res/res_tonedetect.c
+++ b/res/res_tonedetect.c
@@ -367,7 +367,7 @@
 		return 0;
 	}
 
-	if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? &di->rx : &di->tx)) {
+	if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? di->rx : di->tx)) {
 		return 0;
 	}
 
diff --git a/tests/test_vector.c b/tests/test_vector.c
index 2dfcc60..5f6f0a5 100644
--- a/tests/test_vector.c
+++ b/tests/test_vector.c
@@ -48,7 +48,7 @@
 }
 
 #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)

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/18529
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: Ia081ca1bcfb329df6487c4660aaf1944309eb570
Gerrit-Change-Number: 18529
Gerrit-PatchSet: 2
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Michael Bradeen <mbradeen at sangoma.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220509/cc12da06/attachment-0001.html>


More information about the asterisk-code-review mailing list