diff --git a/main/asterisk.c b/main/asterisk.c
index 53e785803fda79cb202b198c952d29b52779a8a9..a5a33d043fe72643ca392e9a7a56ce60440ab4fb 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -1218,7 +1218,7 @@ static void *netconsole(void *vconsole)
 			continue;
 		}
 		if (fds[0].revents) {
-			res = read_credentials(con->fd, tmp, sizeof(tmp), con);
+			res = read_credentials(con->fd, tmp, sizeof(tmp) - 1, con);
 			if (res < 1) {
 				break;
 			}
@@ -2418,7 +2418,7 @@ static char *cli_complete(EditLine *editline, int ch)
 	if (ast_opt_remote) {
 		snprintf(buf, sizeof(buf), "_COMMAND NUMMATCHES \"%s\" \"%s\"", lf->buffer, ptr); 
 		fdsend(ast_consock, buf);
-		res = read(ast_consock, buf, sizeof(buf));
+		res = read(ast_consock, buf, sizeof(buf) - 1);
 		buf[res] = '\0';
 		nummatches = atoi(buf);
 
diff --git a/main/rtp_engine.c b/main/rtp_engine.c
index cf6d2c6f2df53f0e8b601229bf3f6613dc1eab56..9818ea58470b43e43de3a7bd17ff70bd159b2141 100644
--- a/main/rtp_engine.c
+++ b/main/rtp_engine.c
@@ -499,7 +499,7 @@ void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_cod
 
 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
 {
-	if (payload < 0 || payload > AST_RTP_MAX_PT || !static_RTP_PT[payload].code) {
+	if (payload < 0 || payload >= AST_RTP_MAX_PT || !static_RTP_PT[payload].code) {
 		return;
 	}
 
@@ -521,7 +521,7 @@ int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs,
 	unsigned int i;
 	int found = 0;
 
-	if (pt < 0 || pt > AST_RTP_MAX_PT)
+	if (pt < 0 || pt >= AST_RTP_MAX_PT)
 		return -1; /* bogus payload type */
 
 	for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
@@ -569,7 +569,7 @@ int ast_rtp_codecs_payloads_set_rtpmap_type(struct ast_rtp_codecs *codecs, struc
 
 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
 {
-	if (payload < 0 || payload > AST_RTP_MAX_PT) {
+	if (payload < 0 || payload >= AST_RTP_MAX_PT) {
 		return;
 	}
 
@@ -587,7 +587,7 @@ struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs
 {
 	struct ast_rtp_payload_type result = { .asterisk_format = 0, };
 
-	if (payload < 0 || payload > AST_RTP_MAX_PT) {
+	if (payload < 0 || payload >= AST_RTP_MAX_PT) {
 		return result;
 	}
 
diff --git a/main/say.c b/main/say.c
index f43f188c54bc5b4a9e9a0f19c03c18be00676981..5e3ae96d9b6fca9a8d9fdf92397d73bc95f4b6a7 100644
--- a/main/say.c
+++ b/main/say.c
@@ -3560,7 +3560,7 @@ int ast_say_date_with_format_en(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -3807,7 +3807,7 @@ int ast_say_date_with_format_da(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -4009,7 +4009,7 @@ int ast_say_date_with_format_de(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -4406,7 +4406,7 @@ int ast_say_date_with_format_he(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -4546,7 +4546,7 @@ int ast_say_date_with_format_es(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				snprintf(nextmsg, sizeof(nextmsg), "%s", sndfile);
@@ -4735,7 +4735,7 @@ int ast_say_date_with_format_fr(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -4930,7 +4930,7 @@ int ast_say_date_with_format_it(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -5160,7 +5160,7 @@ int ast_say_date_with_format_nl(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -5363,7 +5363,7 @@ int ast_say_date_with_format_pl(struct ast_channel *chan, time_t thetime, const
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset = 0;
-				for (sndoffset = 0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset = 0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -5582,7 +5582,7 @@ int ast_say_date_with_format_pt(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				snprintf(nextmsg, sizeof(nextmsg), "%s", sndfile);
@@ -5874,7 +5874,7 @@ int ast_say_date_with_format_zh(struct ast_channel *chan, time_t t, const char *
 			case '\'':
 				/* Literal name of a sound file */
 				sndoffset=0;
-				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+				for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 					sndfile[sndoffset] = format[offset];
 				sndfile[sndoffset] = '\0';
 				res = wait_file(chan, ints, sndfile, lang);
@@ -7363,7 +7363,7 @@ static int ast_say_date_with_format_gr(struct ast_channel *chan, time_t t, const
 		case '\'':
 			/* Literal name of a sound file */
 			sndoffset=0;
-			for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < 256) ; sndoffset++)
+			for (sndoffset=0 ; (format[++offset] != '\'') && (sndoffset < sizeof(sndfile) - 1) ; sndoffset++)
 				sndfile[sndoffset] = format[offset];
 			sndfile[sndoffset] = '\0';
 			res = wait_file(chan, ints, sndfile, lang);