From 91b4186b8adf158f8044906f12d96c2d819ce529 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 12 Sep 2023 20:36:19 -0500 Subject: [PATCH] Use strtok_r()/strtok_s() instead of regular strtok() --- src/Platform.h | 6 ++++++ src/RtspConnection.c | 3 ++- src/RtspParser.c | 13 +++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Platform.h b/src/Platform.h index 1d60605..0acd2e4 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -52,6 +52,12 @@ # endif #endif +#ifdef LC_WINDOWS +// Windows doesn't have strtok_r() but it has the same +// function named strtok_s(). +#define strtok_r strtok_s +#endif + #include #include "Limelight.h" diff --git a/src/RtspConnection.c b/src/RtspConnection.c index 7dba3e5..2ac8880 100644 --- a/src/RtspConnection.c +++ b/src/RtspConnection.c @@ -972,6 +972,7 @@ int performRtspHandshake(PSERVER_INFORMATION serverInfo) { char* sessionId; char* pingPayload; int error = -1; + char* strtokCtx; if (!setupStream(&response, AppVersionQuad[0] >= 5 ? "streamid=audio/0/0" : "streamid=audio", @@ -1025,7 +1026,7 @@ int performRtspHandshake(PSERVER_INFORMATION serverInfo) { // resolves any 454 session not found errors on // standard RTSP server implementations. // (i.e - sessionId = "DEADBEEFCAFE;timeout = 90") - sessionIdString = strdup(strtok(sessionId, ";")); + sessionIdString = strdup(strtok_r(sessionId, ";", &strtokCtx)); if (sessionIdString == NULL) { Limelog("Failed to duplicate session ID string\n"); ret = -1; diff --git a/src/RtspParser.c b/src/RtspParser.c index 2f1ff29..0593043 100644 --- a/src/RtspParser.c +++ b/src/RtspParser.c @@ -63,6 +63,7 @@ int parseRtspMessage(PRTSP_MESSAGE msg, char* rtspMessage, int length) { char flag; bool messageEnded = false; + char* strtokCtx = NULL; char* payload = NULL; char* opt = NULL; int statusCode = 0; @@ -89,7 +90,7 @@ int parseRtspMessage(PRTSP_MESSAGE msg, char* rtspMessage, int length) { messageBuffer[length] = 0; // Get the first token of the message - token = strtok(messageBuffer, delim); + token = strtok_r(messageBuffer, delim, &strtokCtx); if (token == NULL) { exitCode = RTSP_ERROR_MALFORMED; goto ExitFailure; @@ -102,7 +103,7 @@ int parseRtspMessage(PRTSP_MESSAGE msg, char* rtspMessage, int length) { protocol = token; // Get the status code - token = strtok(NULL, delim); + token = strtok_r(NULL, delim, &strtokCtx); statusCode = atoi(token); if (token == NULL) { exitCode = RTSP_ERROR_MALFORMED; @@ -110,7 +111,7 @@ int parseRtspMessage(PRTSP_MESSAGE msg, char* rtspMessage, int length) { } // Get the status string - statusStr = strtok(NULL, end); + statusStr = strtok_r(NULL, end, &strtokCtx); if (statusStr == NULL) { exitCode = RTSP_ERROR_MALFORMED; goto ExitFailure; @@ -125,12 +126,12 @@ int parseRtspMessage(PRTSP_MESSAGE msg, char* rtspMessage, int length) { else { flag = TYPE_REQUEST; command = token; - target = strtok(NULL, delim); + target = strtok_r(NULL, delim, &strtokCtx); if (target == NULL) { exitCode = RTSP_ERROR_MALFORMED; goto ExitFailure; } - protocol = strtok(NULL, delim); + protocol = strtok_r(NULL, delim, &strtokCtx); if (protocol == NULL) { exitCode = RTSP_ERROR_MALFORMED; goto ExitFailure; @@ -145,7 +146,7 @@ int parseRtspMessage(PRTSP_MESSAGE msg, char* rtspMessage, int length) { // Parse remaining options while (token != NULL) { - token = strtok(NULL, typeFlag == TOKEN_OPTION ? optDelim : end); + token = strtok_r(NULL, typeFlag == TOKEN_OPTION ? optDelim : end, &strtokCtx); if (token != NULL) { if (typeFlag == TOKEN_OPTION) { opt = token;