Handle malloc returning null

Locks are required for multi-threading. If memory can't be allocated, exit the program with memory error. If we let the program continue, it will deadlock in the next part of the code anyway so better end it before.
This commit is contained in:
John Norrbin 2019-01-04 19:24:59 +01:00 committed by GitHub
parent 779ec4dedc
commit 1e848d2e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
/* $OpenBSD: crypto_lock.c,v 1.1 2018/11/11 06:41:28 bcook Exp $ */ /* $OpenBSD: crypto_lock.c,v 1.1 2018/11/11 06:41:28 bcook Exp $ */
/* /*
* Copyright (c) 2018 Brent Cook <bcook@openbsd.org> * Copyright (c) 2019 Brent Cook <bcook@openbsd.org>
* Copyright (c) 2019 John Norrbin <jlnorrbin@johnex.se>
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -29,8 +30,9 @@ CRYPTO_lock(int mode, int type, const char *file, int line)
if (locks[type] == NULL) { if (locks[type] == NULL) {
LPCRITICAL_SECTION lcs = malloc(sizeof(CRITICAL_SECTION)); LPCRITICAL_SECTION lcs = malloc(sizeof(CRITICAL_SECTION));
if (lcs == NULL) exit(ENOMEM);
InitializeCriticalSection(lcs); InitializeCriticalSection(lcs);
if (InterlockedCompareExchangePointer((void **)&locks[type], (void *)lcs, NULL) != NULL) { if (InterlockedCompareExchangePointer((PVOID*)&locks[type], (PVOID)lcs, NULL) != NULL) {
DeleteCriticalSection(lcs); DeleteCriticalSection(lcs);
free(lcs); free(lcs);
} }