mirror of
https://github.com/zenorogue/hyperrogue.git
synced 2025-01-24 16:07:07 +00:00
Merge pull request #220 from jruderman/offsetof1
Use offsetof to reduce UB
This commit is contained in:
commit
6e3471d481
3
Makefile
3
Makefile
@ -83,7 +83,8 @@ ifeq (${TOOLCHAIN},clang)
|
|||||||
CXXFLAGS_STD = -std=c++11
|
CXXFLAGS_STD = -std=c++11
|
||||||
CXXFLAGS_EARLY += -march=native -fPIC
|
CXXFLAGS_EARLY += -march=native -fPIC
|
||||||
CXXFLAGS_EARLY += -W -Wall -Wextra -Werror -pedantic
|
CXXFLAGS_EARLY += -W -Wall -Wextra -Werror -pedantic
|
||||||
CXXFLAGS_EARLY += -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-maybe-uninitialized -Wno-unknown-warning-option -Wno-overloaded-virtual
|
CXXFLAGS_EARLY += -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-maybe-uninitialized -Wno-unknown-warning-option
|
||||||
|
CXXFLAGS_EARLY += -Wno-invalid-offsetof -Wno-overloaded-virtua
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq (${TOOLCHAIN},gcc)
|
ifeq (${TOOLCHAIN},gcc)
|
||||||
|
@ -134,7 +134,7 @@ template<class T> struct connection_table {
|
|||||||
unsigned char *spintable() { return (unsigned char*) (&move_table[full()->degree()]); }
|
unsigned char *spintable() { return (unsigned char*) (&move_table[full()->degree()]); }
|
||||||
|
|
||||||
/** \brief get the full T from the pointer to this connection table */
|
/** \brief get the full T from the pointer to this connection table */
|
||||||
T* full() { T* x = (T*) this; return (T*)((char*)this - ((char*)(&(x->c)) - (char*)x)); }
|
T* full() { return (T*)((char*)this - offsetof(T, c)); }
|
||||||
/** \brief for the edge d, set the `spin` and `mirror` attributes */
|
/** \brief for the edge d, set the `spin` and `mirror` attributes */
|
||||||
void setspin(int d, int spin, bool mirror) {
|
void setspin(int d, int spin, bool mirror) {
|
||||||
unsigned char& c = spintable() [d];
|
unsigned char& c = spintable() [d];
|
||||||
@ -178,10 +178,9 @@ template<class T> struct connection_table {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
template<class T> T* tailored_alloc(int degree) {
|
template<class T> T* tailored_alloc(int degree) {
|
||||||
const T* sample = nullptr;
|
|
||||||
T* result;
|
T* result;
|
||||||
#ifndef NO_TAILORED_ALLOC
|
#ifndef NO_TAILORED_ALLOC
|
||||||
int b = (char*)&sample->c.move_table[degree] + degree - (char*) sample;
|
int b = offsetof(T, c) + offsetof(connection_table<T>, move_table) + sizeof(T*) * degree + degree;
|
||||||
result = (T*) new char[b];
|
result = (T*) new char[b];
|
||||||
new (result) T();
|
new (result) T();
|
||||||
#else
|
#else
|
||||||
|
@ -75,7 +75,7 @@ void set_linux() {
|
|||||||
|
|
||||||
void set_mac() {
|
void set_mac() {
|
||||||
preprocessor = "g++ -E";
|
preprocessor = "g++ -E";
|
||||||
compiler = "g++ -march=native -W -Wall -Wextra -pedantic -Wno-unused-parameter -Wno-implicit-fallthrough -c";
|
compiler = "g++ -march=native -W -Wall -Wextra -pedantic -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-invalid-offsetof -c";
|
||||||
linker = "g++ -o hyper";
|
linker = "g++ -o hyper";
|
||||||
opts = "-DMAC -I/usr/local/include";
|
opts = "-DMAC -I/usr/local/include";
|
||||||
libs = " -L/usr/local/lib -framework AppKit -framework OpenGL -lSDL -lSDLMain -lSDL_gfx -lSDL_mixer -lSDL_ttf -lpng -lpthread -lz";
|
libs = " -L/usr/local/lib -framework AppKit -framework OpenGL -lSDL -lSDLMain -lSDL_gfx -lSDL_mixer -lSDL_ttf -lpng -lpthread -lz";
|
||||||
|
Loading…
Reference in New Issue
Block a user