Skip to content

Commit

Permalink
Hardening (#450)
Browse files Browse the repository at this point in the history
* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update safe_functions.c

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update string_functions.c

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update pcg_basic.c

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update string_functions.c

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update pcg_basic.c

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update shared_header.h

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Fall throughs

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update uthash.h

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

* Update Makefile

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>

---------

Signed-off-by: Ian Hunter <ianfhunter@gmail.com>
  • Loading branch information
ianfhunter committed Dec 2, 2023
1 parent 1f3b4f8 commit 2de7cff
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ OPT=-O3 \
-Wcast-qual -Wdisabled-optimization -Winit-self \
-Wmissing-declarations -Wmissing-include-dirs \
-Wredundant-decls -Wshadow -Wsign-conversion \
-Wundef -Wno-unused -Wformat=2
-Wundef -Wno-unused -Wformat=2 \
-Wconversion -Wimplicit-fallthrough

ifneq ($(shell uname -s), Darwin)
OPT := $(OPT) -Wl,-z,nodlopen -Wl,-z,noexecstack \
-Wl,-z,relro
endif

# -ffast-math # Problematic for Python

# YACC/LEX fails for the following, so disabled:
Expand Down Expand Up @@ -135,7 +141,8 @@ compile:
# MacOS creates warnings for signs.
$(CC) $(CFLAGS) $(CFILES) $(ARC4RANDOM) \
-Wno-error=implicit-function-declaration \
-Wno-sign-conversion -Wno-sign-compare -lm
-Wno-sign-conversion -Wno-sign-compare -lm \
-Wno-implicit-int-conversion


# Shared Lib
Expand Down
4 changes: 2 additions & 2 deletions src/grammar/external/pcg_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void pcg32_srandom(uint64_t seed, uint64_t seq) {
uint32_t pcg32_random_r(pcg32_random_t* rng) {
uint64_t oldstate = rng->state;
rng->state = oldstate * 6364136223846793005ULL + rng->inc;
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
uint32_t rot = oldstate >> 59u;
uint32_t xorshifted = (uint32_t)(((oldstate >> 18u) ^ oldstate) >> 27u);
uint32_t rot = (uint32_t)(oldstate >> 59u);
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}

Expand Down
17 changes: 17 additions & 0 deletions src/grammar/external/uthash.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#define UTHASH_VERSION 2.3.0

#if __has_attribute(__fallthrough__)
# define fallthrough __attribute__((__fallthrough__))
#else
# define fallthrough do {} while (0) /* fallthrough */
#endif

#include <stddef.h> /* ptrdiff_t */
#include <stdlib.h> /* exit */
#include <string.h> /* memcmp, memset, strlen */
Expand Down Expand Up @@ -724,26 +730,37 @@ typedef unsigned char uint8_t;
switch (_hj_k) { \
case 11: \
hashv += ((unsigned)_hj_key[10] << 24); /* FALLTHROUGH */ \
fallthrough; \
case 10: \
hashv += ((unsigned)_hj_key[9] << 16); /* FALLTHROUGH */ \
fallthrough; \
case 9: \
hashv += ((unsigned)_hj_key[8] << 8); /* FALLTHROUGH */ \
fallthrough; \
case 8: \
_hj_j += ((unsigned)_hj_key[7] << 24); /* FALLTHROUGH */ \
fallthrough; \
case 7: \
_hj_j += ((unsigned)_hj_key[6] << 16); /* FALLTHROUGH */ \
fallthrough; \
case 6: \
_hj_j += ((unsigned)_hj_key[5] << 8); /* FALLTHROUGH */ \
fallthrough; \
case 5: \
_hj_j += _hj_key[4]; /* FALLTHROUGH */ \
fallthrough; \
case 4: \
_hj_i += ((unsigned)_hj_key[3] << 24); /* FALLTHROUGH */ \
fallthrough; \
case 3: \
_hj_i += ((unsigned)_hj_key[2] << 16); /* FALLTHROUGH */ \
fallthrough; \
case 2: \
_hj_i += ((unsigned)_hj_key[1] << 8); /* FALLTHROUGH */ \
fallthrough; \
case 1: \
_hj_i += _hj_key[0]; /* FALLTHROUGH */ \
fallthrough; \
default:; \
} \
HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
Expand Down
5 changes: 5 additions & 0 deletions src/grammar/shared_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ extern "C"
#include "rolls/dice_frontend.h"
#include "util/vector_functions.h"

#if __has_attribute(__fallthrough__)
# define fallthrough __attribute__((__fallthrough__))
#else
# define fallthrough do {} while (0) /* fallthrough */
#endif

#define MAX_SYMBOL_LENGTH 256
#define MAX_ITERATION 20
Expand Down
2 changes: 1 addition & 1 deletion src/grammar/util/safe_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void * safe_calloc(size_t nitems, size_t size) {
}
void *calloc_result = NULL;
calloc_result = calloc(nitems, size);
unsigned int total_sz = nitems * size;
long unsigned int total_sz = nitems * size;
if (!calloc_result && total_sz) {
gnoll_errno = BAD_ALLOC;
}
Expand Down
4 changes: 2 additions & 2 deletions src/grammar/util/string_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ char * concat_strings(char ** s, unsigned int num_s){
if (num_s == 1){
return s[0];
}
unsigned int size_total = 0;
long unsigned int size_total = 0;
unsigned int spaces = 0;
for(unsigned int i = 1; i != num_s + 1; i++){
size_total += strlen(s[i]) + 1;
Expand All @@ -25,7 +25,7 @@ char * concat_strings(char ** s, unsigned int num_s){
size_total -= 1; // no need for trailing space
}

printf("Size Total %u\n", size_total);
// printf("Size Total %lu\n", size_total);

char * result = (char *)safe_calloc((size_total+1), sizeof(char));
if(gnoll_errno){return NULL;}
Expand Down

0 comments on commit 2de7cff

Please sign in to comment.