Commit ad5b8ee1a1 for asterisk.org

commit ad5b8ee1a168310224e3699dc91eef7bb4ee2b53
Author: vatss90 <vatss90@gmail.com>
Date:   Tue Nov 3 11:53:05 2026 +0530

    format_mp3: Validate MP3 stream during open

    Validate MP3 streams during open to reject invalid audio data
    and allow the core file helper to try another compatible
    format with the same basename.

    Fixes: #2274

    UserNote: Invalid audio files with an MP3 extension are now
    rejected during stream initialization, allowing Asterisk to
    try another compatible format with the same basename.

diff --git a/addons/format_mp3.c b/addons/format_mp3.c
index f4b0a2ac0b..d5302e01de 100644
--- a/addons/format_mp3.c
+++ b/addons/format_mp3.c
@@ -94,15 +94,108 @@ static const char name[] = "mp3";
 #endif
 #endif

+static int mp3_validate(struct ast_filestream *s)
+{
+	struct mpstr validation_mp;
+	char input[MP3_SCACHE];
+	char output[MP3_DCACHE];
+	int decoded = 0;
+	int result = MP3_NEED_MORE;
+	size_t bytes;
+	long current_pos;
+	int valid = 0;
+
+	current_pos = ftell(s->f);
+	if (current_pos < 0) {
+		ast_log(LOG_WARNING,
+			"Unable to determine MP3 file position\n");
+		return -1;
+	}
+
+	if (!InitMP3(&validation_mp, OUTSCALE)) {
+		ast_log(LOG_WARNING,
+			"Unable to initialize MP3 validation decoder\n");
+		return -1;
+	}
+
+	/*
+	 * The decoder may need more input data to complete an MPEG audio
+	 * frame. Continue reading and feeding data until a frame is
+	 * successfully decoded, a decoding error occurs, or EOF is reached.
+	 */
+	while ((bytes = fread(input, 1, sizeof(input), s->f)) > 0) {
+		decoded = 0;
+
+		result = decodeMP3(&validation_mp, input, bytes,
+			output, sizeof(output), &decoded);
+
+		if (result == MP3_ERR) {
+			break;
+		}
+
+		if (result == MP3_OK && decoded > 0) {
+			valid = 1;
+			break;
+		}
+	}
+
+	if (ferror(s->f)) {
+		ast_log(LOG_WARNING,
+			"Error reading MP3 file during validation: %s\n",
+			strerror(errno));
+		valid = 0;
+	}
+
+	ExitMP3(&validation_mp);
+
+	if (fseek(s->f, current_pos, SEEK_SET)) {
+		ast_log(LOG_WARNING,
+			"Unable to restore MP3 file position\n");
+		return -1;
+	}
+
+	return valid ? 0 : -1;
+}

 static int mp3_open(struct ast_filestream *s)
 {
 	struct mp3_private *p = s->_private;
-	InitMP3(&p->mp, OUTSCALE);
+
+	/*
+	 * Initialize the playback decoder first. If validation fails,
+	 * the stream cleanup callback can safely call mp3_close().
+	 */
+	if (!InitMP3(&p->mp, OUTSCALE)) {
+		ast_log(LOG_WARNING,
+			"Unable to initialize MP3 playback decoder\n");
+		return -1;
+	}
+
+	/*
+	 * Validate the MP3 stream by attempting to decode an MPEG
+	 * audio frame. If validation fails, the core file helper
+	 * can try another compatible format with the same basename.
+	 */
+	if (mp3_validate(s)) {
+		ast_log(LOG_WARNING,
+			"Invalid MP3 stream; unable to decode audio data\n");
+		return -1;
+	}
+
+	/*
+	 * Explicitly reset buffer lengths and offsets before playback
+	 * to ensure the stream starts with a clean state.
+	 */
+	p->sbuflen = 0;
+	p->dbuflen = 0;
+	p->dbufoffset = 0;
+	p->buflen = 0;
+	p->offset = 0;
+	p->seek = 0;
+
 	return 0;
 }

-
 static void mp3_close(struct ast_filestream *s)
 {
 	struct mp3_private *p = s->_private;