src/stdlib/SDL_getenv.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 05 Jan 2015 01:41:42 -0500
changeset 9306 817656bd36ec
parent 8645 d69fdbefeecf
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Clang static analysis builds should use C runtime directly.

This is a little macro magic to use malloc() directly instead of SDL_malloc(),
etc, so static analysis tests that know about the C runtime can function
properly, and understand that we are dealing with heap allocations, etc.

This changed our static analysis report from 5 outstanding bugs to 30.

5x as many bugs were hidden by SDL_malloc() not being recognized as malloc()
by the static analyzer!
slouken@1330
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@1330
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1330
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1330
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1330
    20
*/
icculus@9306
    21
icculus@9306
    22
#if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
icculus@9306
    23
#define SDL_DISABLE_ANALYZE_MACROS 1
icculus@9306
    24
#endif
icculus@9306
    25
icculus@8093
    26
#include "../SDL_internal.h"
slouken@1330
    27
slouken@7828
    28
#if defined(__WIN32__)
slouken@7828
    29
#include "../core/windows/SDL_windows.h"
slouken@7828
    30
#endif
slouken@7828
    31
slouken@1354
    32
#include "SDL_stdinc.h"
slouken@1330
    33
icculus@8092
    34
#if defined(__WIN32__) && (!defined(HAVE_SETENV) || !defined(HAVE_GETENV))
slouken@1330
    35
/* Note this isn't thread-safe! */
slouken@1895
    36
static char *SDL_envmem = NULL; /* Ugh, memory leak */
slouken@1456
    37
static size_t SDL_envmemlen = 0;
icculus@7003
    38
#endif
icculus@7003
    39
icculus@3581
    40
/* Put a variable into the environment */
aschiffler@8645
    41
/* Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/) */
slouken@7351
    42
#if defined(HAVE_SETENV)
slouken@7351
    43
int
slouken@7351
    44
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@7351
    45
{
aschiffler@8645
    46
    /* Input validation */
aschiffler@8645
    47
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
aschiffler@8645
    48
        return (-1);
aschiffler@8645
    49
    }
aschiffler@8645
    50
    
slouken@7351
    51
    return setenv(name, value, overwrite);
slouken@7351
    52
}
icculus@7003
    53
#elif defined(__WIN32__)
slouken@1895
    54
int
icculus@3581
    55
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@1330
    56
{
aschiffler@8645
    57
    /* Input validation */
aschiffler@8645
    58
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
aschiffler@8645
    59
        return (-1);
aschiffler@8645
    60
    }
aschiffler@8645
    61
    
icculus@3581
    62
    if (!overwrite) {
icculus@3581
    63
        char ch = 0;
slouken@5090
    64
        const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch));
icculus@3581
    65
        if (len > 0) {
icculus@3581
    66
            return 0;  /* asked not to overwrite existing value. */
icculus@3581
    67
        }
slouken@1895
    68
    }
slouken@5090
    69
    if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
slouken@1895
    70
        return -1;
slouken@1895
    71
    }
slouken@1895
    72
    return 0;
slouken@1330
    73
}
icculus@7003
    74
/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
icculus@7003
    75
#elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
icculus@7003
    76
int
icculus@7003
    77
SDL_setenv(const char *name, const char *value, int overwrite)
icculus@7003
    78
{
icculus@7003
    79
    size_t len;
icculus@7003
    80
    char *new_variable;
slouken@1330
    81
aschiffler@8645
    82
    /* Input validation */
aschiffler@8645
    83
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
aschiffler@8645
    84
        return (-1);
aschiffler@8645
    85
    }
aschiffler@8645
    86
    
icculus@7003
    87
    if (getenv(name) != NULL) {
icculus@7003
    88
        if (overwrite) {
icculus@7003
    89
            unsetenv(name);
icculus@7003
    90
        } else {
icculus@7003
    91
            return 0;  /* leave the existing one there. */
icculus@7003
    92
        }
icculus@7003
    93
    }
slouken@1330
    94
icculus@7003
    95
    /* This leaks. Sorry. Get a better OS so we don't have to do this. */
icculus@7003
    96
    len = SDL_strlen(name) + SDL_strlen(value) + 2;
icculus@7003
    97
    new_variable = (char *) SDL_malloc(len);
icculus@7003
    98
    if (!new_variable) {
icculus@7003
    99
        return (-1);
slouken@1895
   100
    }
icculus@7003
   101
icculus@7003
   102
    SDL_snprintf(new_variable, len, "%s=%s", name, value);
icculus@7003
   103
    return putenv(new_variable);
slouken@1330
   104
}
slouken@1330
   105
#else /* roll our own */
slouken@1895
   106
static char **SDL_env = (char **) 0;
slouken@1895
   107
int
icculus@3581
   108
SDL_setenv(const char *name, const char *value, int overwrite)
slouken@1330
   109
{
slouken@1895
   110
    int added;
slouken@1895
   111
    int len, i;
slouken@1895
   112
    char **new_env;
slouken@1895
   113
    char *new_variable;
slouken@1330
   114
aschiffler@8645
   115
    /* Input validation */
aschiffler@8645
   116
    if (!name || SDL_strlen(name) == 0 || SDL_strchr(name, '=') != NULL || !value) {
slouken@1895
   117
        return (-1);
slouken@1895
   118
    }
slouken@1330
   119
slouken@3583
   120
    /* See if it already exists */
slouken@3583
   121
    if (!overwrite && SDL_getenv(name)) {
slouken@3583
   122
        return 0;
slouken@3583
   123
    }
slouken@3583
   124
slouken@1895
   125
    /* Allocate memory for the variable */
icculus@3581
   126
    len = SDL_strlen(name) + SDL_strlen(value) + 2;
icculus@3581
   127
    new_variable = (char *) SDL_malloc(len);
slouken@1895
   128
    if (!new_variable) {
slouken@1895
   129
        return (-1);
slouken@1895
   130
    }
slouken@1330
   131
icculus@3581
   132
    SDL_snprintf(new_variable, len, "%s=%s", name, value);
icculus@3581
   133
    value = new_variable + SDL_strlen(name) + 1;
icculus@3581
   134
    name = new_variable;
icculus@3581
   135
slouken@1895
   136
    /* Actually put it into the environment */
slouken@1895
   137
    added = 0;
slouken@1895
   138
    i = 0;
slouken@1895
   139
    if (SDL_env) {
slouken@1895
   140
        /* Check to see if it's already there... */
slouken@1895
   141
        len = (value - name);
slouken@1895
   142
        for (; SDL_env[i]; ++i) {
slouken@1895
   143
            if (SDL_strncmp(SDL_env[i], name, len) == 0) {
slouken@1895
   144
                break;
slouken@1895
   145
            }
slouken@1895
   146
        }
slouken@1895
   147
        /* If we found it, just replace the entry */
slouken@1895
   148
        if (SDL_env[i]) {
slouken@1895
   149
            SDL_free(SDL_env[i]);
slouken@1895
   150
            SDL_env[i] = new_variable;
slouken@1895
   151
            added = 1;
slouken@1895
   152
        }
slouken@1895
   153
    }
slouken@1330
   154
slouken@1895
   155
    /* Didn't find it in the environment, expand and add */
slouken@1895
   156
    if (!added) {
slouken@1895
   157
        new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
slouken@1895
   158
        if (new_env) {
slouken@1895
   159
            SDL_env = new_env;
slouken@1895
   160
            SDL_env[i++] = new_variable;
slouken@1895
   161
            SDL_env[i++] = (char *) 0;
slouken@1895
   162
            added = 1;
slouken@1895
   163
        } else {
slouken@1895
   164
            SDL_free(new_variable);
slouken@1895
   165
        }
slouken@1895
   166
    }
slouken@1895
   167
    return (added ? 0 : -1);
slouken@1330
   168
}
icculus@7003
   169
#endif
slouken@1330
   170
slouken@1330
   171
/* Retrieve a variable named "name" from the environment */
slouken@7351
   172
#if defined(HAVE_GETENV)
slouken@7351
   173
char *
slouken@7351
   174
SDL_getenv(const char *name)
slouken@7351
   175
{
aschiffler@8645
   176
    /* Input validation */
aschiffler@8645
   177
    if (!name || SDL_strlen(name)==0) {
aschiffler@8645
   178
        return NULL;
aschiffler@8645
   179
    }
aschiffler@8645
   180
slouken@7351
   181
    return getenv(name);
slouken@7351
   182
}
icculus@7003
   183
#elif defined(__WIN32__)
icculus@7003
   184
char *
icculus@7003
   185
SDL_getenv(const char *name)
icculus@7003
   186
{
icculus@7003
   187
    size_t bufferlen;
icculus@7003
   188
aschiffler@8645
   189
    /* Input validation */
aschiffler@8645
   190
    if (!name || SDL_strlen(name)==0) {
aschiffler@8645
   191
        return NULL;
aschiffler@8645
   192
    }
aschiffler@8645
   193
    
icculus@7003
   194
    bufferlen =
icculus@7003
   195
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
icculus@7003
   196
    if (bufferlen == 0) {
icculus@7003
   197
        return NULL;
icculus@7003
   198
    }
icculus@7003
   199
    if (bufferlen > SDL_envmemlen) {
icculus@7003
   200
        char *newmem = (char *) SDL_realloc(SDL_envmem, bufferlen);
icculus@7003
   201
        if (newmem == NULL) {
icculus@7003
   202
            return NULL;
icculus@7003
   203
        }
icculus@7003
   204
        SDL_envmem = newmem;
icculus@7003
   205
        SDL_envmemlen = bufferlen;
icculus@7003
   206
        GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen);
icculus@7003
   207
    }
icculus@7003
   208
    return SDL_envmem;
icculus@7003
   209
}
icculus@7003
   210
#else
slouken@1895
   211
char *
slouken@1895
   212
SDL_getenv(const char *name)
slouken@1330
   213
{
slouken@1895
   214
    int len, i;
slouken@1895
   215
    char *value;
slouken@1330
   216
aschiffler@8645
   217
    /* Input validation */
aschiffler@8645
   218
    if (!name || SDL_strlen(name)==0) {
aschiffler@8645
   219
        return NULL;
aschiffler@8645
   220
    }
aschiffler@8645
   221
    
slouken@1895
   222
    value = (char *) 0;
slouken@1895
   223
    if (SDL_env) {
slouken@1895
   224
        len = SDL_strlen(name);
slouken@1895
   225
        for (i = 0; SDL_env[i] && !value; ++i) {
slouken@1895
   226
            if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
slouken@1895
   227
                (SDL_env[i][len] == '=')) {
slouken@1895
   228
                value = &SDL_env[i][len + 1];
slouken@1895
   229
            }
slouken@1895
   230
        }
slouken@1895
   231
    }
slouken@1895
   232
    return value;
slouken@1330
   233
}
icculus@3581
   234
#endif
icculus@3581
   235
icculus@3581
   236
slouken@1330
   237
#ifdef TEST_MAIN
slouken@1330
   238
#include <stdio.h>
slouken@1330
   239
slouken@1895
   240
int
slouken@1895
   241
main(int argc, char *argv[])
slouken@1330
   242
{
slouken@1895
   243
    char *value;
slouken@1330
   244
slouken@1895
   245
    printf("Checking for non-existent variable... ");
slouken@1895
   246
    fflush(stdout);
slouken@1895
   247
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   248
        printf("okay\n");
slouken@1895
   249
    } else {
slouken@1895
   250
        printf("failed\n");
slouken@1895
   251
    }
slouken@1895
   252
    printf("Setting FIRST=VALUE1 in the environment... ");
slouken@1895
   253
    fflush(stdout);
slouken@3591
   254
    if (SDL_setenv("FIRST", "VALUE1", 0) == 0) {
slouken@1895
   255
        printf("okay\n");
slouken@1895
   256
    } else {
slouken@1895
   257
        printf("failed\n");
slouken@1895
   258
    }
slouken@1895
   259
    printf("Getting FIRST from the environment... ");
slouken@1895
   260
    fflush(stdout);
slouken@1895
   261
    value = SDL_getenv("FIRST");
slouken@1895
   262
    if (value && (SDL_strcmp(value, "VALUE1") == 0)) {
slouken@1895
   263
        printf("okay\n");
slouken@1895
   264
    } else {
slouken@1895
   265
        printf("failed\n");
slouken@1895
   266
    }
slouken@1895
   267
    printf("Setting SECOND=VALUE2 in the environment... ");
slouken@1895
   268
    fflush(stdout);
slouken@3591
   269
    if (SDL_setenv("SECOND", "VALUE2", 0) == 0) {
slouken@1895
   270
        printf("okay\n");
slouken@1895
   271
    } else {
slouken@1895
   272
        printf("failed\n");
slouken@1895
   273
    }
slouken@1895
   274
    printf("Getting SECOND from the environment... ");
slouken@1895
   275
    fflush(stdout);
slouken@1895
   276
    value = SDL_getenv("SECOND");
slouken@1895
   277
    if (value && (SDL_strcmp(value, "VALUE2") == 0)) {
slouken@1895
   278
        printf("okay\n");
slouken@1895
   279
    } else {
slouken@1895
   280
        printf("failed\n");
slouken@1895
   281
    }
slouken@1895
   282
    printf("Setting FIRST=NOVALUE in the environment... ");
slouken@1895
   283
    fflush(stdout);
slouken@3591
   284
    if (SDL_setenv("FIRST", "NOVALUE", 1) == 0) {
slouken@1895
   285
        printf("okay\n");
slouken@1895
   286
    } else {
slouken@1895
   287
        printf("failed\n");
slouken@1895
   288
    }
slouken@1895
   289
    printf("Getting FIRST from the environment... ");
slouken@1895
   290
    fflush(stdout);
slouken@1895
   291
    value = SDL_getenv("FIRST");
slouken@1895
   292
    if (value && (SDL_strcmp(value, "NOVALUE") == 0)) {
slouken@1895
   293
        printf("okay\n");
slouken@1895
   294
    } else {
slouken@1895
   295
        printf("failed\n");
slouken@1895
   296
    }
slouken@1895
   297
    printf("Checking for non-existent variable... ");
slouken@1895
   298
    fflush(stdout);
slouken@1895
   299
    if (!SDL_getenv("EXISTS")) {
slouken@1895
   300
        printf("okay\n");
slouken@1895
   301
    } else {
slouken@1895
   302
        printf("failed\n");
slouken@1895
   303
    }
slouken@1895
   304
    return (0);
slouken@1330
   305
}
slouken@1330
   306
#endif /* TEST_MAIN */
slouken@3591
   307
slouken@1895
   308
/* vi: set ts=4 sw=4 expandtab: */