Skip to content

Commit

Permalink
[#1823] format code
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Sep 17, 2024
1 parent 3dc1284 commit 04a9a73
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 57 deletions.
58 changes: 29 additions & 29 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,38 +298,38 @@ void *OQS_MEM_checked_aligned_alloc(size_t alignment, size_t size) {
OQS_API void OQS_MEM_secure_free(void *ptr, size_t len) {
if (ptr != NULL) {
OQS_MEM_cleanse(ptr, len);
OQS_MEM_free(ptr); // IGNORE free-check
OQS_MEM_free(ptr); // IGNORE free-check
}
}

OQS_API void OQS_MEM_insecure_free(void *ptr) {
OQS_MEM_free(ptr); // IGNORE free-check
OQS_MEM_free(ptr); // IGNORE free-check
}

void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) {
#if defined(OQS_USE_OPENSSL)
// Use OpenSSL's memory allocation functions
if (!size) {
return NULL;
}
const size_t offset = alignment - 1 + sizeof(uint8_t);
uint8_t *buffer = OPENSSL_malloc(size + offset);
if (!buffer) {
return NULL;
}
uint8_t *ptr = (uint8_t *)(((uintptr_t)(buffer) + offset) & ~(alignment - 1));
ptrdiff_t diff = ptr - buffer;
if (diff > UINT8_MAX) {
// Free and return NULL if alignment is too large
OPENSSL_free(buffer);
errno = EINVAL;
return NULL;
}
// Store the difference so that the free function can use it
ptr[-1] = diff;
return ptr;
// Use OpenSSL's memory allocation functions
if (!size) {
return NULL;
}
const size_t offset = alignment - 1 + sizeof(uint8_t);
uint8_t *buffer = OPENSSL_malloc(size + offset);
if (!buffer) {
return NULL;
}
uint8_t *ptr = (uint8_t *)(((uintptr_t)(buffer) + offset) & ~(alignment - 1));
ptrdiff_t diff = ptr - buffer;
if (diff > UINT8_MAX) {
// Free and return NULL if alignment is too large
OPENSSL_free(buffer);
errno = EINVAL;
return NULL;
}
// Store the difference so that the free function can use it
ptr[-1] = diff;
return ptr;
#elif defined(OQS_HAVE_ALIGNED_ALLOC) // glibc and other implementations providing aligned_alloc
return aligned_alloc(alignment, size);
return aligned_alloc(alignment, size);
#else
// Check alignment (power of 2, and >= sizeof(void*)) and size (multiple of alignment)
if (alignment & (alignment - 1) || size & (alignment - 1) || alignment < sizeof(void *)) {
Expand Down Expand Up @@ -391,13 +391,13 @@ void *OQS_MEM_aligned_alloc(size_t alignment, size_t size) {

void OQS_MEM_aligned_free(void *ptr) {
#if defined(OQS_USE_OPENSSL)
// Use OpenSSL's free function
if (ptr) {
uint8_t *u8ptr = ptr;
OPENSSL_free(u8ptr - u8ptr[-1]);
}
// Use OpenSSL's free function
if (ptr) {
uint8_t *u8ptr = ptr;
OPENSSL_free(u8ptr - u8ptr[-1]);
}
#elif defined(OQS_HAVE_ALIGNED_ALLOC) || defined(OQS_HAVE_POSIX_MEMALIGN) || defined(OQS_HAVE_MEMALIGN)
free(ptr); // IGNORE free-check
free(ptr); // IGNORE free-check
#elif defined(__MINGW32__) || defined(__MINGW64__)
__mingw_aligned_free(ptr);
#elif defined(_MSC_VER)
Expand Down
48 changes: 24 additions & 24 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,39 @@ extern "C" {
#endif

#if defined(OQS_USE_OPENSSL)
#ifndef OPENSSL_malloc
# define OPENSSL_malloc(num) \
#ifndef OPENSSL_malloc
# define OPENSSL_malloc(num) \
CRYPTO_malloc(num, __FILE__, __LINE__)
#endif
#endif

#ifndef OPENSSL_zalloc
# define OPENSSL_zalloc(num) \
#ifndef OPENSSL_zalloc
# define OPENSSL_zalloc(num) \
CRYPTO_zalloc(num, __FILE__, __LINE__)
#endif
#endif

#ifndef OPENSSL_free
# define OPENSSL_free(addr) \
#ifndef OPENSSL_free
# define OPENSSL_free(addr) \
CRYPTO_free(addr, __FILE__, __LINE__)
#endif
#endif

#ifndef OPENSSL_strdup
# define OPENSSL_strdup(str) \
#ifndef OPENSSL_strdup
# define OPENSSL_strdup(str) \
CRYPTO_strdup(str, __FILE__, __LINE__)
#endif
#endif

extern void *CRYPTO_malloc(size_t num, const char *file, int line);
extern void *CRYPTO_zalloc(size_t num, const char *file, int line);
extern void CRYPTO_free(void *str, const char *file, int line);
extern char *CRYPTO_strdup(const char *str, const char* file, int line);
#define OQS_MEM_malloc(size) OPENSSL_malloc(size)
#define OQS_MEM_free(ptr) OPENSSL_free(ptr)
#define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size))
#define OQS_MEM_strdup(str) OPENSSL_strdup(str)
extern void *CRYPTO_malloc(size_t num, const char *file, int line);
extern void *CRYPTO_zalloc(size_t num, const char *file, int line);
extern void CRYPTO_free(void *str, const char *file, int line);
extern char *CRYPTO_strdup(const char *str, const char *file, int line);
#define OQS_MEM_malloc(size) OPENSSL_malloc(size)
#define OQS_MEM_free(ptr) OPENSSL_free(ptr)
#define OQS_MEM_calloc(num_elements, element_size) OPENSSL_zalloc((num_elements) * (element_size))
#define OQS_MEM_strdup(str) OPENSSL_strdup(str)
#else
#define OQS_MEM_malloc(size) malloc(size)
#define OQS_MEM_free(ptr) free(ptr)
#define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size)
#define OQS_MEM_strdup(str) strdup(str)
#define OQS_MEM_malloc(size) malloc(size)
#define OQS_MEM_free(ptr) free(ptr)
#define OQS_MEM_calloc(num_elements, element_size) calloc(num_elements, element_size)
#define OQS_MEM_strdup(str) strdup(str)
#endif

/**
Expand Down
8 changes: 4 additions & 4 deletions src/common/sha2/sha2_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,22 +588,22 @@ void oqs_sha2_sha512_inc_ctx_clone_c(sha512ctx *stateout, const sha512ctx *state

/* Destroy the hash state. */
void oqs_sha2_sha224_inc_ctx_release_c(sha224ctx *state) {
OQS_MEM_free(state->ctx); // IGNORE free-check
OQS_MEM_free(state->ctx); // IGNORE free-check
}

/* Destroy the hash state. */
void oqs_sha2_sha256_inc_ctx_release_c(sha256ctx *state) {
OQS_MEM_free(state->ctx); // IGNORE free-check
OQS_MEM_free(state->ctx); // IGNORE free-check
}

/* Destroy the hash state. */
void oqs_sha2_sha384_inc_ctx_release_c(sha384ctx *state) {
OQS_MEM_free(state->ctx); // IGNORE free-check
OQS_MEM_free(state->ctx); // IGNORE free-check
}

/* Destroy the hash state. */
void oqs_sha2_sha512_inc_ctx_release_c(sha512ctx *state) {
OQS_MEM_free(state->ctx); // IGNORE free-check
OQS_MEM_free(state->ctx); // IGNORE free-check
}

void oqs_sha2_sha256_inc_blocks_c(sha256ctx *state, const uint8_t *in, size_t inblocks) {
Expand Down

0 comments on commit 04a9a73

Please sign in to comment.