From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 26238C7115B for ; Mon, 23 Jun 2025 12:56:20 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [45.14.194.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id A0AC0601F1; Mon, 23 Jun 2025 14:56:08 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz A0AC0601F1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1750683378; bh=fYYfARf/tCieNKzkwcoElWi8/WegeqoBwdnu3qk6e7c=; h=From:To:In-Reply-To:References:Subject:Date:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From; b=nCdX9I6Xt6Xv61kW1pGH0tXwabcZpj4yqfsCi6p4p2S3Vr3JQsDk4lcSy/y1O0NoL PVFl2+q3rV8KoQN8sRlh7iJqq9303AGFsx6cKL3HkPMk+0ikxR3kzzJiNYnNcGpUSW XkNhvPA91R+Gb9JCidMQdd4Y5TIubmCMiLVsoV7M= Received: by alsa1.perex.cz (Postfix, from userid 50401) id 89692F805C5; Mon, 23 Jun 2025 14:55:43 +0200 (CEST) Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id 7BC01F805C0; Mon, 23 Jun 2025 14:55:43 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 293F8F80424; Mon, 23 Jun 2025 14:55:40 +0200 (CEST) Received: from webhooks-bot.alsa-project.org (vmi2259423.contaboserver.net [45.14.194.44]) by alsa1.perex.cz (Postfix) with ESMTP id 7A8DBF8013D for ; Mon, 23 Jun 2025 14:55:37 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 7A8DBF8013D MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit From: GitHub issues - opened To: alsa-devel@alsa-project.org Message-Id: <184bad38693da800-webhooks-bot@alsa-project.org> In-Reply-To: <184bad3869381000-webhooks-bot@alsa-project.org> References: <184bad3869381000-webhooks-bot@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) Message-ID-Hash: BAGK7NCDJT6I375BRKECNDC72I4UVD5X X-Message-ID-Hash: BAGK7NCDJT6I375BRKECNDC72I4UVD5X X-MailFrom: github@alsa-project.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-alsa-devel.alsa-project.org-0; header-match-alsa-devel.alsa-project.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.9 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 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