From 5b7933fe5e47ad0bc333b405f554c20f8cc2efed Mon Sep 17 00:00:00 2001
From: Brett Bryant <bbryant@digium.com>
Date: Fri, 11 Jul 2008 18:09:35 +0000
Subject: [PATCH] Janitor patch to change uses of sizeof to ARRAY_LEN

(closes issue #13054)
Reported by: pabelanger
Patches:
      ARRAY_LEN.patch2 uploaded by pabelanger (license 224)
Tested by: seanbright


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@130129 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_chanspy.c      |  4 ++--
 apps/app_milliwatt.c    |  2 +-
 apps/app_minivm.c       |  8 ++++----
 channels/chan_dahdi.c   | 12 ++++++------
 channels/chan_gtalk.c   |  4 ++--
 channels/chan_jingle.c  |  4 ++--
 channels/chan_sip.c     |  6 +++---
 channels/chan_skinny.c  |  2 +-
 codecs/codec_dahdi.c    |  4 ++--
 codecs/codec_g722.c     |  4 ++--
 codecs/codec_resample.c |  4 ++--
 main/abstract_jb.c      |  2 +-
 main/asterisk.c         |  6 +++---
 main/cli.c              |  2 +-
 main/dsp.c              |  4 ++--
 main/threadstorage.c    |  2 +-
 main/utils.c            |  2 +-
 pbx/dundi-parser.c      |  2 +-
 utils/astman.c          |  4 ++--
 19 files changed, 39 insertions(+), 39 deletions(-)

diff --git a/apps/app_chanspy.c b/apps/app_chanspy.c
index 7a89efd805..b570169cb2 100644
--- a/apps/app_chanspy.c
+++ b/apps/app_chanspy.c
@@ -754,12 +754,12 @@ static int common_exec(struct ast_channel *chan, struct ast_flags *flags,
 			if (mygroup) {
 				dup_mygroup = ast_strdupa(mygroup);
 				num_mygroups = ast_app_separate_args(dup_mygroup, ':', mygroups,
-					sizeof(mygroups) / sizeof(mygroups[0]));
+					ARRAY_LEN(mygroups));
 
 				if ((group = pbx_builtin_getvar_helper(peer, "SPYGROUP"))) {
 					dup_group = ast_strdupa(group);
 					num_groups = ast_app_separate_args(dup_group, ':', groups,
-						sizeof(groups) / sizeof(groups[0]));
+						ARRAY_LEN(groups));
 				}
 
 				for (y = 0; y < num_mygroups; y++) {
diff --git a/apps/app_milliwatt.c b/apps/app_milliwatt.c
index 0fcd72151c..89063096cd 100644
--- a/apps/app_milliwatt.c
+++ b/apps/app_milliwatt.c
@@ -64,7 +64,7 @@ static void milliwatt_release(struct ast_channel *chan, void *data)
 static int milliwatt_generate(struct ast_channel *chan, void *data, int len, int samples)
 {
 	unsigned char buf[AST_FRIENDLY_OFFSET + 640];
-	const int maxsamples = sizeof (buf) / sizeof (buf[0]);
+	const int maxsamples = ARRAY_LEN(buf);
 	int i, *indexp = (int *) data;
 	struct ast_frame wf = {
 		.frametype = AST_FRAME_VOICE,
diff --git a/apps/app_minivm.c b/apps/app_minivm.c
index 8b507ddd70..cd6e84c02d 100644
--- a/apps/app_minivm.c
+++ b/apps/app_minivm.c
@@ -1641,7 +1641,7 @@ static int minivm_notify_exec(struct ast_channel *chan, void *data)
 		ast_log(LOG_ERROR, "Out of memory\n");
 		return -1;
 	}
-	argc = ast_app_separate_args(tmpptr, ',', argv, sizeof(argv) / sizeof(argv[0]));
+	argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
 
 	if (argc == 2 && !ast_strlen_zero(argv[1]))
 		template = argv[1];
@@ -1721,7 +1721,7 @@ static int minivm_record_exec(struct ast_channel *chan, void *data)
 		ast_log(LOG_ERROR, "Out of memory\n");
 		return -1;
 	}
-	argc = ast_app_separate_args(tmp, ',', argv, sizeof(argv) / sizeof(argv[0]));
+	argc = ast_app_separate_args(tmp, ',', argv, ARRAY_LEN(argv));
 	if (argc == 2) {
 		if (ast_app_parse_options(minivm_app_options, &flags, opts, argv[1])) {
 			return -1;
@@ -1783,7 +1783,7 @@ static int minivm_greet_exec(struct ast_channel *chan, void *data)
 		ast_log(LOG_ERROR, "Out of memory\n");
 		return -1;
 	}
-	argc = ast_app_separate_args(tmpptr, ',', argv, sizeof(argv) / sizeof(argv[0]));
+	argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
 
 	if (argc == 2) {
 		if (ast_app_parse_options(minivm_app_options, &flags, opts, argv[1]))
@@ -2005,7 +2005,7 @@ static int minivm_accmess_exec(struct ast_channel *chan, void *data)
 			ast_log(LOG_ERROR, "Out of memory\n");
 			error = TRUE;
 		} else
-			argc = ast_app_separate_args(tmpptr, ',', argv, sizeof(argv) / sizeof(argv[0]));
+			argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
 	}
 
 	if (argc <=1) {
diff --git a/channels/chan_dahdi.c b/channels/chan_dahdi.c
index bd68addf10..7fa99a6c0d 100644
--- a/channels/chan_dahdi.c
+++ b/channels/chan_dahdi.c
@@ -1357,7 +1357,7 @@ static char *alarm2str(int alarm)
 static char *event2str(int event)
 {
 	static char buf[256];
-	if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
+	if ((event < (ARRAY_LEN(events))) && (event > -1))
 		return events[event];
 	sprintf(buf, "Event %d", event); /* safe */
 	return buf;
@@ -6925,7 +6925,7 @@ static void *ss_thread(void *data)
 									break;
 								/* Increment the ringT counter so we can match it against
 								   values in chan_dahdi.conf for distinctive ring */
-								if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
+								if (++receivedRingT == ARRAY_LEN(curRingData))
 									break;
 							} else if (i & DAHDI_IOMUX_READ) {
 								res = read(p->subs[index].zfd, buf, sizeof(buf));
@@ -7084,7 +7084,7 @@ static void *ss_thread(void *data)
 							break;
 						/* Increment the ringT counter so we can match it against
 						   values in chan_dahdi.conf for distinctive ring */
-						if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
+						if (++receivedRingT == ARRAY_LEN(curRingData))
 							break;
 					} else if (i & DAHDI_IOMUX_READ) {
 						res = read(p->subs[index].zfd, buf, sizeof(buf));
@@ -7145,7 +7145,7 @@ static void *ss_thread(void *data)
 								break;
 							/* Increment the ringT counter so we can match it against
 							   values in chan_dahdi.conf for distinctive ring */
-							if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
+							if (++receivedRingT == ARRAY_LEN(curRingData))
 								break;
 						} else if (i & DAHDI_IOMUX_READ) {
 							res = read(p->subs[index].zfd, buf, sizeof(buf));
@@ -13701,7 +13701,7 @@ static void process_echocancel(struct dahdi_chan_conf *confp, const char *data,
 	unsigned int param_count;
 	unsigned int x;
 
-	if (!(param_count = ast_app_separate_args(parse, ',', params, sizeof(params) / sizeof(params[0]))))
+	if (!(param_count = ast_app_separate_args(parse, ',', params, ARRAY_LEN(params)))
 		return;
 
 	memset(&confp->chan.echocancel, 0, sizeof(confp->chan.echocancel));
@@ -14809,7 +14809,7 @@ static int load_module(void)
 	ast_cli_register_multiple(dahdi_pri_cli, sizeof(dahdi_pri_cli) / sizeof(struct ast_cli_entry));
 #endif	
 #ifdef HAVE_SS7
-	ast_cli_register_multiple(dahdi_ss7_cli, sizeof(dahdi_ss7_cli) / sizeof(dahdi_ss7_cli[0]));
+	ast_cli_register_multiple(dahdi_ss7_cli, ARRAY_LEN(dahdi_ss7_cli));
 #endif
 
 	ast_cli_register_multiple(dahdi_cli, sizeof(dahdi_cli) / sizeof(struct ast_cli_entry));
diff --git a/channels/chan_gtalk.c b/channels/chan_gtalk.c
index f8b038bf85..d42afaff7d 100644
--- a/channels/chan_gtalk.c
+++ b/channels/chan_gtalk.c
@@ -2012,7 +2012,7 @@ static int load_module(void)
 	}
 
 	ast_rtp_proto_register(&gtalk_rtp);
-	ast_cli_register_multiple(gtalk_cli, sizeof(gtalk_cli) / sizeof(gtalk_cli[0]));
+	ast_cli_register_multiple(gtalk_cli, ARRAY_LEN(gtalk_cli));
 
 	/* Make sure we can register our channel type */
 	if (ast_channel_register(&gtalk_tech)) {
@@ -2032,7 +2032,7 @@ static int reload(void)
 static int unload_module(void)
 {
 	struct gtalk_pvt *privates = NULL;
-	ast_cli_unregister_multiple(gtalk_cli, sizeof(gtalk_cli) / sizeof(gtalk_cli[0]));
+	ast_cli_unregister_multiple(gtalk_cli, ARRAY_LEN(gtalk_cli));
 	/* First, take us out of the channel loop */
 	ast_channel_unregister(&gtalk_tech);
 	ast_rtp_proto_unregister(&gtalk_rtp);
diff --git a/channels/chan_jingle.c b/channels/chan_jingle.c
index 107db08010..43b178426b 100644
--- a/channels/chan_jingle.c
+++ b/channels/chan_jingle.c
@@ -1879,7 +1879,7 @@ static int load_module(void)
 	}
 
 	ast_rtp_proto_register(&jingle_rtp);
-	ast_cli_register_multiple(jingle_cli, sizeof(jingle_cli) / sizeof(jingle_cli[0]));
+	ast_cli_register_multiple(jingle_cli, ARRAY_LEN(jingle_cli));
 	/* Make sure we can register our channel type */
 	if (ast_channel_register(&jingle_tech)) {
 		ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
@@ -1898,7 +1898,7 @@ static int reload(void)
 static int unload_module(void)
 {
 	struct jingle_pvt *privates = NULL;
-	ast_cli_unregister_multiple(jingle_cli, sizeof(jingle_cli) / sizeof(jingle_cli[0]));
+	ast_cli_unregister_multiple(jingle_cli, ARRAY_LEN(jingle_cli));
 	/* First, take us out of the channel loop */
 	ast_channel_unregister(&jingle_tech);
 	ast_rtp_proto_unregister(&jingle_rtp);
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 0d2743ca91..245d757176 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -2601,7 +2601,7 @@ static int find_sip_method(const char *msg)
 	
 	if (ast_strlen_zero(msg))
 		return 0;
-	for (i = 1; i < (sizeof(sip_methods) / sizeof(sip_methods[0])) && !res; i++) {
+	for (i = 1; i < (ARRAY_LEN(sip_methods)) && !res; i++) {
 		if (method_match(i, msg))
 			res = sip_methods[i].id;
 	}
@@ -13640,7 +13640,7 @@ static const char *subscription_type2str(enum subscriptiontype subtype)
 {
 	int i;
 
-	for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
+	for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
 		if (subscription_types[i].type == subtype) {
 			return subscription_types[i].text;
 		}
@@ -13653,7 +13653,7 @@ static const struct cfsubscription_types *find_subscription_type(enum subscripti
 {
 	int i;
 
-	for (i = 1; (i < (sizeof(subscription_types) / sizeof(subscription_types[0]))); i++) {
+	for (i = 1; i < ARRAY_LEN(subscription_types); i++) {
 		if (subscription_types[i].type == subtype) {
 			return &subscription_types[i];
 		}
diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c
index 83b1b45232..8263a79e6c 100644
--- a/channels/chan_skinny.c
+++ b/channels/chan_skinny.c
@@ -6423,7 +6423,7 @@ static int load_module(void)
 {
 	int res = 0;
 
-	for (; res < (sizeof(soft_key_template_default) / sizeof(soft_key_template_default[0])); res++) {
+	for (; res < ARRAY_LEN(soft_key_template_default); res++) {
 		soft_key_template_default[res].softKeyEvent = htolel(soft_key_template_default[res].softKeyEvent);
 	}
 	/* load and parse config */
diff --git a/codecs/codec_dahdi.c b/codecs/codec_dahdi.c
index bc13d787cb..2de40f9962 100644
--- a/codecs/codec_dahdi.c
+++ b/codecs/codec_dahdi.c
@@ -466,7 +466,7 @@ static int reload(void)
 
 static int unload_module(void)
 {
-	ast_cli_unregister_multiple(cli, sizeof(cli) / sizeof(cli[0]));
+	ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
 	unregister_translators();
 
 	return 0;
@@ -477,7 +477,7 @@ static int load_module(void)
 	if (parse_config(0))
 		return AST_MODULE_LOAD_DECLINE;
 	find_transcoders();
-	ast_cli_register_multiple(cli, sizeof(cli) / sizeof(cli[0]));
+	ast_cli_register_multiple(cli, ARRAY_LEN(cli));
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
diff --git a/codecs/codec_g722.c b/codecs/codec_g722.c
index 19e6b33b1e..b221ab20fd 100644
--- a/codecs/codec_g722.c
+++ b/codecs/codec_g722.c
@@ -166,7 +166,7 @@ static struct ast_frame *lintog722_sample (void)
 		.frametype = AST_FRAME_VOICE,
 		.subclass = AST_FORMAT_SLINEAR,
 		.datalen = sizeof(slin_g722_ex),
-		.samples = sizeof(slin_g722_ex) / sizeof(slin_g722_ex[0]),
+		.samples = ARRAY_LEN(slin_g722_ex),
 		.src = __PRETTY_FUNCTION__,
 		.data.ptr = slin_g722_ex,
 	};
@@ -180,7 +180,7 @@ static struct ast_frame *lin16tog722_sample (void)
 		.frametype = AST_FRAME_VOICE,
 		.subclass = AST_FORMAT_SLINEAR16,
 		.datalen = sizeof(slin_g722_ex),
-		.samples = sizeof(slin_g722_ex) / sizeof(slin_g722_ex[0]),
+		.samples = ARRAY_LEN(slin_g722_ex),
 		.src = __PRETTY_FUNCTION__,
 		.data.ptr = slin_g722_ex,
 	};
diff --git a/codecs/codec_resample.c b/codecs/codec_resample.c
index f0ae03307f..023b9b60d6 100644
--- a/codecs/codec_resample.c
+++ b/codecs/codec_resample.c
@@ -166,7 +166,7 @@ static struct ast_frame *slin16_to_slin8_sample(void)
 		.frametype = AST_FRAME_VOICE,
 		.subclass = AST_FORMAT_SLINEAR16,
 		.datalen = sizeof(slin16_slin8_ex),
-		.samples = sizeof(slin16_slin8_ex) / sizeof(slin16_slin8_ex[0]),
+		.samples = ARRAY_LEN(slin16_slin8_ex),
 		.src = __PRETTY_FUNCTION__,
 		.data.ptr = slin16_slin8_ex,
 	};
@@ -180,7 +180,7 @@ static struct ast_frame *slin8_to_slin16_sample(void)
 		.frametype = AST_FRAME_VOICE,
 		.subclass = AST_FORMAT_SLINEAR,
 		.datalen = sizeof(slin8_slin16_ex),
-		.samples = sizeof(slin8_slin16_ex) / sizeof(slin8_slin16_ex[0]),
+		.samples = ARRAY_LEN(slin8_slin16_ex),
 		.src = __PRETTY_FUNCTION__,
 		.data.ptr = slin8_slin16_ex,
 	};
diff --git a/main/abstract_jb.c b/main/abstract_jb.c
index d7f9a57b7d..4b04279a9d 100644
--- a/main/abstract_jb.c
+++ b/main/abstract_jb.c
@@ -176,7 +176,7 @@ static void jb_choose_impl(struct ast_channel *chan)
 	struct ast_jb *jb = &chan->jb;
 	struct ast_jb_conf *jbconf = &jb->conf;
 	struct ast_jb_impl *test_impl;
-	int i, avail_impl_count = sizeof(avail_impl) / sizeof(avail_impl[0]);
+	int i, avail_impl_count = ARRAY_LEN(avail_impl);
 	
 	jb->impl = &avail_impl[default_impl];
 	
diff --git a/main/asterisk.c b/main/asterisk.c
index 739bf3dea5..c836611fce 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -2910,9 +2910,9 @@ int main(int argc, char *argv[])
 	char *remotesock = NULL;
 
 	/* Remember original args for restart */
-	if (argc > sizeof(_argv) / sizeof(_argv[0]) - 1) {
-		fprintf(stderr, "Truncating argument size to %d\n", (int)(sizeof(_argv) / sizeof(_argv[0])) - 1);
-		argc = sizeof(_argv) / sizeof(_argv[0]) - 1;
+	if (argc > ARRAY_LEN(_argv) - 1) {
+		fprintf(stderr, "Truncating argument size to %d\n", (int)ARRAY_LEN(_argv) - 1);
+		argc = ARRAY_LEN(_argv) - 1;
 	}
 	for (x = 0; x < argc; x++)
 		_argv[x] = argv[x];
diff --git a/main/cli.c b/main/cli.c
index 1335ec6b7c..af8b923c14 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -1777,7 +1777,7 @@ static char *__ast_cli_generator(const char *text, const char *word, int state,
 	char matchstr[80] = "";
 	int tws = 0;
 	/* Split the argument into an array of words */
-	char *dup = parse_args(text, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws);
+	char *dup = parse_args(text, &x, argv, ARRAY_LEN(argv), &tws);
 
 	if (!dup)	/* malloc error */
 		return NULL;
diff --git a/main/dsp.c b/main/dsp.c
index f9c8a2d945..f3adcf0bef 100644
--- a/main/dsp.c
+++ b/main/dsp.c
@@ -393,7 +393,7 @@ struct ast_dsp {
 
 static void mute_fragment(struct ast_dsp *dsp, fragment_t *fragment)
 {
-	if (dsp->mute_fragments >= sizeof(dsp->mute_data) / sizeof(dsp->mute_data[0])) {
+	if (dsp->mute_fragments >= ARRAY_LEN(dsp->mute_data)) {
 		ast_log(LOG_ERROR, "Too many fragments to mute. Ignoring\n");
 		return;
 	}
@@ -1440,7 +1440,7 @@ static void ast_dsp_prog_reset(struct ast_dsp *dsp)
 	
 	dsp->gsamp_size = modes[dsp->progmode].size;
 	dsp->gsamps = 0;
-	for (x = 0; x < sizeof(modes[dsp->progmode].freqs) / sizeof(modes[dsp->progmode].freqs[0]); x++) {
+	for (x = 0; x < ARRAY_LEN(modes[dsp->progmode].freqs); x++) {
 		if (modes[dsp->progmode].freqs[x]) {
 			goertzel_init(&dsp->freqs[x], (float)modes[dsp->progmode].freqs[x], dsp->gsamp_size);
 			max = x + 1;
diff --git a/main/threadstorage.c b/main/threadstorage.c
index ae1719ff84..94ea45d4ea 100644
--- a/main/threadstorage.c
+++ b/main/threadstorage.c
@@ -232,7 +232,7 @@ static struct ast_cli_entry cli[] = {
 
 void threadstorage_init(void)
 {
-	ast_cli_register_multiple(cli, sizeof(cli) / sizeof(cli[0]));
+	ast_cli_register_multiple(cli, ARRAY_LEN(cli));
 }
 
 #endif /* !defined(DEBUG_THREADLOCALS) */
diff --git a/main/utils.c b/main/utils.c
index c164c55880..f156770ca9 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -1723,7 +1723,7 @@ int ast_utils_init(void)
 	base64_init();
 #ifdef DEBUG_THREADS
 #if !defined(LOW_MEMORY)
-	ast_cli_register_multiple(utils_cli, sizeof(utils_cli) / sizeof(utils_cli[0]));
+	ast_cli_register_multiple(utils_cli, ARRAY_LEN(utils_cli));
 #endif
 #endif
 	return 0;
diff --git a/pbx/dundi-parser.c b/pbx/dundi-parser.c
index b37c6905f8..50c45e08da 100644
--- a/pbx/dundi-parser.c
+++ b/pbx/dundi-parser.c
@@ -181,7 +181,7 @@ static void dump_cause(char *output, int maxlen, void *value, int len)
 	memcpy(tmp2, cause->desc, datalen);
 	tmp2[datalen] = '\0';
 
-	if (causecode < sizeof(causes) / sizeof(causes[0])) {
+	if (causecode < ARRAY_LEN(causes)) {
 		if (ast_strlen_zero(tmp2))
 			snprintf(output, maxlen, "%s", causes[causecode]);
 		else
diff --git a/utils/astman.c b/utils/astman.c
index f674105a52..6c8d74291b 100644
--- a/utils/astman.c
+++ b/utils/astman.c
@@ -258,14 +258,14 @@ static int process_message(struct ast_mansession *s, struct message *m)
 		fprintf(stderr, "Missing event in request");
 		return 0;
 	}
-	for (x=0;x<sizeof(events) / sizeof(events[0]);x++) {
+	for (x = 0; x < ARRAY_LEN(events); x++) {
 		if (!strcasecmp(event, events[x].event)) {
 			if (events[x].func(s, m))
 				return -1;
 			break;
 		}
 	}
-	if (x >= sizeof(events) / sizeof(events[0]))
+	if (x >= ARRAY_LEN(events))
 		fprintf(stderr, "Ignoring unknown event '%s'", event);
 #if 0
 	for (x=0;x<m->hdrcount;x++) {
-- 
GitLab