Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strict aliasing warnings from voipchat.c #93

Open
sezero opened this issue May 1, 2024 · 0 comments
Open

strict aliasing warnings from voipchat.c #93

sezero opened this issue May 1, 2024 · 0 comments

Comments

@sezero
Copy link
Contributor

sezero commented May 1, 2024

@icculus: I get strict-aliasing warnings from voipchat.c from gcc <= 4.9:

voipchat.c: In function 'SendClientAudioToServer':
voipchat.c:94:9: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
         ((Uint64 *) scratch_area)[0] = SDL_SwapLE64(0);  /* just being nice and leaving space in the buffer for the server to replace. */
         ^
voipchat.c: In function 'mainloop':
voipchat.c:228:17: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
                 ((Uint64 *) scratch_area)[0] = SDL_SwapLE64(0);
                 ^

The following fixes it for me, and the asm output from gcc-7.5 before and after are identical.
(Asm outputs from gcc 4.4 are also identical if I use -fno-strict-aliasing.) OK to apply?

diff --git a/examples/voipchat.c b/examples/voipchat.c
index 145e2ea..8952fc1 100644
--- a/examples/voipchat.c
+++ b/examples/voipchat.c
@@ -36,9 +36,9 @@ static SDL_Renderer *renderer = NULL;
 static SDL_AudioDeviceID audio_device = 0;
 static SDL_AudioDeviceID capture_device = 0;
 static SDL_AudioStream *capture_stream = NULL;
 static const SDL_AudioSpec audio_spec = { SDL_AUDIO_S16LE, 1, 8000 };
-static Uint8 scratch_area[4096];
+static Uint64 scratch_area[512];
 
 static Voice *FindVoiceByAddr(const SDLNet_Address *addr, const Uint16 port)
 {
     Voice *i;
@@ -88,12 +88,12 @@ static void ClearOldVoices(const Uint64 now)
 
 static const int extra = (int) (sizeof (Uint64) * 2);
 static void SendClientAudioToServer(void)
 {
-    const int br = SDL_GetAudioStreamData(capture_stream, scratch_area + extra, max_datagram - extra);
+    const int br = SDL_GetAudioStreamData(capture_stream, scratch_area + (extra / sizeof(Uint64)), max_datagram - extra);
     if (br > 0) {
-        ((Uint64 *) scratch_area)[0] = SDL_SwapLE64(0);  /* just being nice and leaving space in the buffer for the server to replace. */
-        ((Uint64 *) scratch_area)[1] = SDL_SwapLE64(++next_idnum);
+        scratch_area[0] = SDL_SwapLE64(0);  /* just being nice and leaving space in the buffer for the server to replace. */
+        scratch_area[1] = SDL_SwapLE64(++next_idnum);
         SDL_Log("CLIENT: Sending %d new bytes to server at %s:%d...", br + extra, SDLNet_GetAddressString(server_addr), (int) server_port);
         SDLNet_SendDatagram(sock, server_addr, server_port, scratch_area, br + extra);
     }
 }
@@ -224,10 +224,10 @@ static void mainloop(void)
                 }
             }
 
             if (!last_send_ticks || ((now - last_send_ticks) > 5000)) {  /* send a keepalive packet if we haven't transmitted for a bit. */
-                ((Uint64 *) scratch_area)[0] = SDL_SwapLE64(0);
-                ((Uint64 *) scratch_area)[1] = SDL_SwapLE64(++next_idnum);
+                scratch_area[0] = SDL_SwapLE64(0);
+                scratch_area[1] = SDL_SwapLE64(++next_idnum);
                 SDL_Log("CLIENT: Sending %d keepalive bytes to server at %s:%d...", extra, SDLNet_GetAddressString(server_addr), (int) server_port);
                 SDLNet_SendDatagram(sock, server_addr, server_port, scratch_area, extra);
                 last_send_ticks = now;
             }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant