src/thread/windows/SDL_systls.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jul 2013 21:57:31 -0700
changeset 7396 a36ab6149a10
parent 7393 358696c354a8
child 7828 1451063c8ecd
permissions -rw-r--r--
Fixed bug 1953 - Crash at memcpy X11_DispatchEvent(_THIS) Function Nitz In Function X11_DispatchEvent(_THIS), case SelectionNotify : static void X11_DispatchEvent(_THIS) { // Some Code case SelectionNotify: { //Some Code SDL_bool expect_lf = SDL_FALSE; char *start = NULL; // Initialised with NULL char *scan = (char*)p.data; char *fn; char *uri; int length = 0; while (p.count--) { if (!expect_lf) { if (*scan==0x0D) { expect_lf = SDL_TRUE; } else if(start == NULL) { start = scan; length = 0; } length++; } else { if (*scan==0x0A && length>0) { uri = malloc(length--); memcpy(uri, start, length); // Problem is Here, start is still NULL if control comes to else statement without initialising the start pointer, which is wrong uri[length] = 0; fn = X11_URIToLocal(uri); if (fn) SDL_SendDropFile(fn); free(uri); } expect_lf = SDL_FALSE; start = NULL; } scan++; } } As shown above how start pointer remains NULL, Patch for this issue would be: if (*scan==0x0D) { expect_lf = SDL_TRUE; } if(start == NULL) { start = scan; length = 0; } Just replace else if statement with if.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
  Simple DirectMedia Layer
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
  arising from the use of this software.
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
     appreciated but is not required.
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
     misrepresented as being the original software.
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
#include "SDL_config.h"
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
#include "SDL_thread.h"
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    24
#include "../SDL_thread_c.h"
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#if SDL_THREAD_WINDOWS
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "../../core/windows/SDL_windows.h"
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
static DWORD thread_local_storage = TLS_OUT_OF_INDEXES;
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    32
static SDL_bool generic_local_storage = SDL_FALSE;
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    34
SDL_TLSData *
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    35
SDL_SYS_GetTLSData()
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
{
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    37
    if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    38
        static SDL_SpinLock lock;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    39
        SDL_AtomicLock(&lock);
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    40
        if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    41
            DWORD storage = TlsAlloc();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    42
            if (storage != TLS_OUT_OF_INDEXES) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    43
                SDL_MemoryBarrierRelease();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    44
                thread_local_storage = storage;
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    45
            } else {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    46
                generic_local_storage = SDL_TRUE;
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
            }
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
        }
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    49
        SDL_AtomicUnlock(&lock);
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    }
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    51
    if (generic_local_storage) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    52
        return SDL_Generic_GetTLSData();
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
    }
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    54
    SDL_MemoryBarrierAcquire();
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    55
    return (SDL_TLSData *)TlsGetValue(thread_local_storage);
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
}
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
int
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    59
SDL_SYS_SetTLSData(SDL_TLSData *data)
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
{
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    61
    if (generic_local_storage) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    62
        return SDL_Generic_SetTLSData(data);
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
    }
7393
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    64
    if (!TlsSetValue(thread_local_storage, data)) {
358696c354a8 Added release/acquire memory barriers to the atomic API
Sam Lantinga <slouken@libsdl.org>
parents: 7391
diff changeset
    65
        return SDL_SetError("TlsSetValue() failed");
7391
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
    }
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
    return 0;
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
}
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
#endif /* SDL_THREAD_WINDOWS */
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
a29895dc5e9a Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
/* vi: set ts=4 sw=4 expandtab: */