Skip to content

Don't use strtok(3) #276

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kubernetes/ConfigureChecks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ include(CheckSymbolExists)
check_symbol_exists(strndup "string.h" HAVE_STRNDUP)
check_symbol_exists(secure_getenv "stdlib.h" HAVE_SECURE_GETENV)
check_symbol_exists(getenv "stdlib.h" HAVE_GETENV)
check_symbol_exists(strtok_r "string.h" HAVE_STRTOK_R)
3 changes: 2 additions & 1 deletion kubernetes/config.h.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#cmakedefine HAVE_STRNDUP
#cmakedefine HAVE_SECURE_GETENV
#cmakedefine HAVE_GETENV
#cmakedefine HAVE_GETENV
#cmakedefine HAVE_STRTOK_R
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ static time_t get_token_expiration_time(const char *token_string)
}

char *p = NULL;
p = strtok(dup_token_string, OIDC_ID_TOKEN_DELIM); /* jwt header */
char *last = NULL;
p = strtok_r(dup_token_string, OIDC_ID_TOKEN_DELIM, &last); /* jwt header */
if (!p) {
fprintf(stderr, "%s: The token <%s> is not a valid JWT token.\n", fname, token_string);
goto end;
}
p = strtok(NULL, OIDC_ID_TOKEN_DELIM); /* jwt part2 */
p = strtok_r(NULL, OIDC_ID_TOKEN_DELIM, &last); /* jwt part2 */
if (!p) {
fprintf(stderr, "%s: The token <%s> is not a valid JWT token.\n", fname, token_string);
goto end;
Expand Down
4 changes: 4 additions & 0 deletions kubernetes/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ extern "C" {
char *strndup(const char *s, size_t n);
#endif /* ! HAVE_STRNDUP */

#if !defined(HAVE_STRTOK_R)
char *strtok_r(char *str, const char *delim, char **saveptr);
#endif /* ! HAVE_STRTOK_R */

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 9 additions & 1 deletion kubernetes/src/utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include "../include/utils.h"
#include <string.h>
#include "config.h"

// based on https://github.com/libssh/libssh-mirror/commit/247983e9820fd264cb5a59c14cc12846c028bd08#diff-744295d01685fa411dbfd78679ea20b51dfa4ac7d2d722df53f3d86d728493f8
#if !defined(HAVE_STRNDUP)
Expand All @@ -22,4 +23,11 @@ char *strndup(const char *s, size_t n)

return x;
}
#endif /* ! HAVE_STRNDUP */
#endif /* ! HAVE_STRNDUP */

#if !defined(HAVE_STRTOK_R)
char *strtok_r(char *str, const char *delim, char **saveptr)
{
return strtok(str, delim);
}
#endif /* ! HAVE_STRTOK_R */
6 changes: 4 additions & 2 deletions kubernetes/watch/watch_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

static int wu_convert_to_json_array(list_t * json_array, const char *json_string)
{
char *last = NULL;

if (!json_string || '\0' == json_string[0] || !json_array) {
return -1;
}
Expand All @@ -18,7 +20,7 @@ static int wu_convert_to_json_array(list_t * json_array, const char *json_string
char *json_string_dup = strdup(json_string);

char *token = NULL;
token = strtok(json_string_dup, JSON_ARRAY_DELIM);
token = strtok_r(json_string_dup, JSON_ARRAY_DELIM, &last);
while (token) {
cJSON *cjson = cJSON_Parse(token);
if (cjson == NULL) {
Expand All @@ -27,7 +29,7 @@ static int wu_convert_to_json_array(list_t * json_array, const char *json_string
}
cJSON_Delete(cjson);
list_addElement(json_array, strdup(token));
token = strtok(NULL, JSON_ARRAY_DELIM);
token = strtok_r(NULL, JSON_ARRAY_DELIM, &last);
}

end:
Expand Down