PortAudio / portaudio

PortAudio is a cross-platform, open-source C language library for real-time audio input and output.
Other
1.37k stars 286 forks source link

`PaStream` being `void` is type-unsafe and error-prone #915

Open dechamps opened 1 month ago

dechamps commented 1 month ago

In dechamps/FlexASIO@59f75ce183431f8b50f7f209790090c5a608b08e, I introduced a severe bug (dechamps/FlexASIO#231) because I accidentally wrote:

Pa_GetStreamInfo(&stream)

Instead of:

Pa_GetStreamInfo(stream)

It is very sad that the obviously wrong code was able to compile.

The reason why that code managed to sift through the cracks is because PaStream* is defined as void*:

https://github.com/PortAudio/portaudio/blob/18a606e1f928852bfc29639d9539ae74d37b5dee/include/portaudio.h#L639

The problem with void* is that any pointer can be implicitly converted to it, making it very type-unsafe. You could write the following and the compiler won't bat an eye:

int a;
Pa_GetStreamInfo(&a);

I believe it would be more developer-friendly to make PaStream a type-safe opaque type, for example:

typedef struct PaStream PaStream;

This change shouldn't break anyone as long as user code does not rely on PaStream specifically being an alias to void - which seems unlikely. If it does break it's a compile-time error so it shouldn't take anyone by surprise.