Language: C
Label: safe
CWE: CWE-362 \u2014 Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.

Code:
create_new_keys(const char* jwkdir)
{
    const char* alg[] = {"ES512", "ECMR", NULL};
    char path[PATH_MAX];

    /* Set default umask for file creation. */
    umask(0337);
    for (int i = 0; alg[i] != NULL; i++) {
        json_auto_t* jwk = jwk_generate(alg[i]);
        if (!jwk) {
            return 0;
        }
        __attribute__ ((__cleanup__(cleanup_str))) char* thp = jwk_thumbprint(jwk, DEFAULT_THP_HASH);
        if (!thp) {
            return 0;
        }
        if (snprintf(path, PATH_MAX, "%s/%s.jwk", jwkdir, thp) < 0) {
            fprintf(stderr, "Unable to prepare variable with file full path (%s)\n", thp);
            return 0;
        }
        path[sizeof(path) - 1] = '\0';
        if (json_dump_file(jwk, path, 0) == -1) {
            fprintf(stderr, "Error saving JWK to file (%s)\n", path);
            return 0;
        }

        /* Set 0440 permission for the new key. */
        if (chmod(path, S_IRUSR | S_IRGRP) == -1) {
            fprintf(stderr, "Unable to set permissions for JWK file (%s)\n", path);
            return 0;
        }
    }
    return 1;
}