From: GitHub issues - opened <github@alsa-project.org>
To: alsa-devel@alsa-project.org
Subject: Call to snd_pcm_hw_params_get_buffer_size causes memory corruption in calling program
Date: Mon, 23 Jun 2025 14:55:40 +0200 (CEST) [thread overview]
Message-ID: <184bad38693da800-webhooks-bot@alsa-project.org> (raw)
In-Reply-To: <184bad3869381000-webhooks-bot@alsa-project.org>
alsa-project/alsa-lib issue #461 was opened from harryrowe01:
Calling snd_pcm_hw_params_get_buffer_size is causing memory corruption in the calling program.
The following C program demonstrates the issue.
/*
This example opens the default PCM device, sets
some parameters, and then displays the value
of some of the hardware parameters. It does not
perform any sound playback or recording.
*/
/* Use the newer ALSA API */
#define ALSA_PCM_NEW_HW_PARAMS_API
/* All of the ALSA library API is defined
* in this header */
#include <alsa/asoundlib.h>
int main() {
int rc;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params, *save_params;
unsigned int val, val2;
int dir;
snd_pcm_uframes_t frames;
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default",
SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
fprintf(stderr,
"unable to open pcm device: %s\n",
snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params,
SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 2);
/* 44100 bits/second sampling rate (CD quality) */
val = 44100;
// val = 8000;
snd_pcm_hw_params_set_rate_near(handle,
params, &val, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr,
"unable to set hw parameters: %s\n",
snd_strerror(rc));
exit(1);
}
/* Display information about the PCM interface */
// Before calling snd_pcm_hw_params_get_buffer_size, save
// a copy of params which points to the parameter block.
save_params = params;
// This call to snd_pcm_hw_params_get_buffer_size causes
// the value of params in main to be corrupted.
// Since the params argument to the function is passed
// by value, the function itself should not have access to
// the original variable in main.
snd_pcm_hw_params_get_buffer_size(params,
(snd_pcm_uframes_t *)&val);
// Then if the pointer has been modified, flag the problem and
// restore the pointer from the saved value.
if( params != save_params )
{
printf( "The pointer to the parameter block has been corrupted!\n" );
printf( "was %lx \n", save_params );
printf( "is now %lx \n", params );
printf( "So I'm fixing it.\n" );
params = save_params;
}
printf("buffer size = %u frames\n", val);
snd_pcm_close(handle);
return 0;
}
Compiled and run as follows:
harry@idun:~/Documents/morse$ cc bad_alsa.c -lasound -o bad_alsa
harry@idun:~/Documents/morse$ ./bad_alsa
The pointer to the parameter block has been corrupted!
was 7ffcd2011610
is now 7ffc00000000
So I'm fixing it.
buffer size = 1048576 frames
harry@idun:~/Documents/morse$ cc bad_alsa.c -lasound -o bad_alsa
harry@idun:~/Documents/morse$ ./bad_alsa
The pointer to the parameter block has been corrupted!
was 7fffc7483030
is now 7fff00000000
So I'm fixing it.
buffer size = 1048576 frames
harry@idun:~/Documents/morse$
Host is an AMD64 version of Debian 12. Installed version of ALSA library is libasound2 1.2.8-1+b1
Issue URL : https://github.com/alsa-project/alsa-lib/issues/461
Repository URL: https://github.com/alsa-project/alsa-lib
parent reply other threads:[~2025-06-23 12:56 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <184bad3869381000-webhooks-bot@alsa-project.org>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=184bad38693da800-webhooks-bot@alsa-project.org \
--to=github@alsa-project.org \
--cc=alsa-devel@alsa-project.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.