Skip to content

Conversation

haesbaert
Copy link

strtok(3) is not re-entrant, use strtok_r(3).

--

strtok(3) will break on multithreaded programs, basically anyone using the watch callback would break on multithreaded.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: haesbaert
Once this PR has been reviewed and has the lgtm label, please assign brendandburns for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

linux-foundation-easycla bot commented Jul 24, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Welcome @haesbaert!

It looks like this is your first PR to kubernetes-client/c 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/c has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jul 24, 2025
static time_t get_token_expiration_time(const char *token_string)
{
static char fname[] = "get_token_expiration_time()";
char *last;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please set to explicit NULL


static int wu_convert_to_json_array(list_t * json_array, const char *json_string)
{
char *last;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too wrt NULL

@brendandburns
Copy link
Contributor

small request for explicit NULL for the last variables, otherwise LGTM.

You also need to sign the CLA.

@ityuhui
Copy link
Member

ityuhui commented Jul 26, 2025

strtok_r is not implemented on Windows.
Use conditional compilation to avoid build failures on Windows

For example, in this project:

#ifndef _WIN32

or

check_symbol_exists(secure_getenv "stdlib.h" HAVE_SECURE_GETENV)

#if defined(HAVE_SECURE_GETENV)

@haesbaert
Copy link
Author

small request for explicit NULL for the last variables, otherwise LGTM.

You also need to sign the CLA.

Fixed the nit, I need to get approval for the CLA, will come back to you, meanwhile I fixed another bug, see below.

@haesbaert
Copy link
Author

strtok_r is not implemented on Windows. Use conditional compilation to avoid build failures on Windows

For example, in this project:

#ifndef _WIN32

or

check_symbol_exists(secure_getenv "stdlib.h" HAVE_SECURE_GETENV)

#if defined(HAVE_SECURE_GETENV)

Thanks, I've added a check for HAVE_STRTOK_R, which also made me realize the test for HAVE_STRNDUP was broken as utils.c never included "config.h". I'm falling back from strtok_r to strtok, if that's not good enough, I'd suggest copying the one from OpenBSD since it's simple enough and it's BSD3 licensed.

@ityuhui
Copy link
Member

ityuhui commented Jul 29, 2025

Hi @haesbaert

Thanks for the PR! I recommend:

check_symbol_exists(secure_getenv "stdlib.h" HAVE_STRTOK_R)

    char *p = NULL;
    char *last = NULL;
#if defined(HAVE_STRTOK_R)
    p = strtok_r(dup_token_string, OIDC_ID_TOKEN_DELIM, &last);  /* jwt header */
#else
    p = strtok(dup_token_string, OIDC_ID_TOKEN_DELIM); /* jwt header */
#endif
    if (!p) {
        fprintf(stderr, "%s: The token <%s> is not a valid JWT token.\n", fname, token_string);
        goto end;
    }
#if defined(HAVE_STRTOK_R)
    p = strtok_r(NULL, OIDC_ID_TOKEN_DELIM, &last);  /* jwt part2 */
#else
    p = strtok(NULL, OIDC_ID_TOKEN_DELIM); /* jwt part2 */
#endif
    if (!p) {
        fprintf(stderr, "%s: The token <%s> is not a valid JWT token.\n", fname, token_string);
        goto end;

Just like HAVE_SECURE_GETENV and HAVE_GETENV do.

What do you think ?

strtok(3) is not re-entrant, use strtok_r(3), since windows doesn't have it,
guard it under HAVE_STRTOK_R on the same scheme as HAVE_STRNCPY.

While adding HAVE_STRTOK_R I noticed utils.c was not including config.h, which
also made the HAVE_STRNCPY test fail, so fix it as well.
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 8, 2025
@haesbaert
Copy link
Author

Hi @haesbaert

Thanks for the PR! I recommend:

check_symbol_exists(secure_getenv "stdlib.h" HAVE_STRTOK_R)

    char *p = NULL;
    char *last = NULL;
#if defined(HAVE_STRTOK_R)
    p = strtok_r(dup_token_string, OIDC_ID_TOKEN_DELIM, &last);  /* jwt header */
#else
    p = strtok(dup_token_string, OIDC_ID_TOKEN_DELIM); /* jwt header */
#endif
    if (!p) {
        fprintf(stderr, "%s: The token <%s> is not a valid JWT token.\n", fname, token_string);
        goto end;
    }
#if defined(HAVE_STRTOK_R)
    p = strtok_r(NULL, OIDC_ID_TOKEN_DELIM, &last);  /* jwt part2 */
#else
    p = strtok(NULL, OIDC_ID_TOKEN_DELIM); /* jwt part2 */
#endif
    if (!p) {
        fprintf(stderr, "%s: The token <%s> is not a valid JWT token.\n", fname, token_string);
        goto end;

Just like HAVE_SECURE_GETENV and HAVE_GETENV do.

What do you think ?

Thanks for getting back and apologies for the long delay, got busy and there was some holidays.
I've applied your suggestions, let me know if this is what you had in mind.

@@ -1,3 +1,4 @@
#include "config.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a build error due to this including. https://github.com/kubernetes-client/c/actions/runs/17549510255/job/50128037985?pr=276

maybe need to include the relative path of config.h or change the CMakeLists.txt in the oidc directory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included the src directory in 4bd8a7d
I'm not sure how to trigger a build that builds that, so I couldn't properly test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build still fails.

You can build locally using the following command.

cd kubernetes/config/authn_plugin/plugins/oidc
mkdir build
cd build
cmake ..
make

@ityuhui
Copy link
Member

ityuhui commented Sep 11, 2025

Hi @haesbaert

Welcome back ! and thanks for making these changes.
I have a few minor comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants