diff --git a/.vscode/settings.json b/.vscode/settings.json index 8410cf2..78b3015 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -58,5 +58,9 @@ "binaryArgs": ["region", "0", "0"] } ], - "C_Cpp.default.compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe" + "C_Cpp.default.compilerPath": "/usr/bin/gcc", + "C_Cpp.codeAnalysis.clangTidy.checks.disabled": [ + "clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling", + "clang-analyzer-security.insecureAPI.strcpy" + ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 5f320ab..6a6ed27 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,35 +1,11 @@ { "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: gcc-11 build active file", - "command": "/usr/bin/gcc-11", - "args": [ - "-fdiagnostics-color=always", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}", - "-lz" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build" - }, - "detail": "Task generated by Debugger." - }, { "type": "cppbuild", "label": "C/C++: make", "command": "make", "args": [ - "all", - "CFLAGS='-g -Wall -Wpedantic'" + "debug" ], "options": { "cwd": "${fileDirname}" diff --git a/Makefile b/Makefile index 591911f..1f910c6 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,12 @@ ifneq (,$(findstring MSYS,$(UNAME))) SUBUNIT = endif +all: CFLAGS := -O3 all: radiusGenerator modelGenerator chunkExtractor regionFileReader +debug: CFLAGS := -Wall -Werror -Wpedantic +debug: radiusGenerator modelGenerator chunkExtractor regionFileReader + cNBT.o: gcc cNBT/buffer.c -o cNBT/buffer.o -c $(CFLAGS) gcc cNBT/nbt_parsing.c -o cNBT/nbt_parsing.o -c $(CFLAGS) @@ -69,10 +73,10 @@ model.ow: model.c x86_64-w64-mingw32-gcc-win32 model.c -o model.ow -c $(CFLAGS) regionFileReader.exe: regionParser.ow regionFileReader.c - x86_64-w64-mingw32-gcc-win32 regionFileReader.c regionParser.ow -o regionFileReader.exe Wl,-Bstatic -lz -Wl,-Bdynamic $(CFLAGS) + x86_64-w64-mingw32-gcc-win32 regionFileReader.c regionParser.ow -o regionFileReader.exe -lz -static $(CFLAGS) chunkExtractor.exe: regionParser.ow chunkExtractor.c - x86_64-w64-mingw32-gcc-win32 chunkExtractor.c regionParser.ow -o chunkExtractor.exe Wl,-Bstatic -lz -Wl,-Bdynamic $(CFLAGS) + x86_64-w64-mingw32-gcc-win32 chunkExtractor.c regionParser.ow -o chunkExtractor.exe -lz -static $(CFLAGS) generator.ow: generator.c x86_64-w64-mingw32-gcc-win32 generator.c -o generator.ow -c $(CFLAGS) @@ -81,7 +85,7 @@ modelGenerator.exe: generator.ow model.ow chunkParser.ow hTable.ow cNBT.ow model x86_64-w64-mingw32-gcc-win32 modelGenerator.c generator.ow model.ow chunkParser.ow hTable.ow cNBT.ow -o modelGenerator.exe -lm $(CFLAGS) radiusGenerator.exe: model.ow generator.ow hTable.ow chunkParser.ow regionParser.ow cNBT.ow radiusGenerator.c - x86_64-w64-mingw32-gcc-win32 radiusGenerator.c generator.ow model.ow regionParser.ow hTable.ow chunkParser.ow cNBT.ow Wl,-Bstatic -lz -Wl,-Bdynamic -lm -o radiusGenerator.exe $(CFLAGS) + x86_64-w64-mingw32-gcc-win32 radiusGenerator.c generator.ow model.ow regionParser.ow hTable.ow chunkParser.ow cNBT.ow -lm -static -lz -o radiusGenerator.exe $(CFLAGS) windows: modelGenerator.exe chunkExtractor.exe regionFileReader.exe radiusGenerator.exe diff --git a/chunkParser.c b/chunkParser.c index 48aa12e..d701f7d 100644 --- a/chunkParser.c +++ b/chunkParser.c @@ -55,7 +55,7 @@ int getSections(unsigned char* nbtFileData, long sz, struct section* sections){ nbtTagError("sections"); } struct nbt_list* sectionsList = sectionsNode->payload.tag_list; - struct list_head* pos = §ionsList->entry; + struct list_head* pos; int n = 0; //foreach section list_for_each(pos, §ionsList->entry){ @@ -77,7 +77,7 @@ int getSections(unsigned char* nbtFileData, long sz, struct section* sections){ //get the individual block data nbt_node* blockData = nbt_find_by_name(blockNode, "data"); if(blockData != NULL){ - newSection.blockData = malloc(blockData->payload.tag_long_array.length * sizeof(int64_t)); + newSection.blockData = (uint64_t*)malloc(blockData->payload.tag_long_array.length * sizeof(uint64_t)); memcpy(newSection.blockData, blockData->payload.tag_long_array.data, blockData->payload.tag_long_array.length * sizeof(int64_t)); newSection.blockDataLen = blockData->payload.tag_long_array.length; } @@ -93,7 +93,7 @@ int getSections(unsigned char* nbtFileData, long sz, struct section* sections){ //const struct list_head* paletteHead = &palette->payload.tag_list->entry; char** blockPalette = malloc(1 * sizeof(char*)); int i = 0; - struct list_head* paletteCur = &palette->payload.tag_list->entry; + struct list_head* paletteCur; //foreach element in palette list_for_each(paletteCur, &palette->payload.tag_list->entry){ //get the list entry @@ -252,7 +252,7 @@ bool contains(char** arr, char* str, int arrLen){ //this code feels wrong to write in C ngl... char** createGlobalPalette(struct section* sections, int len, int* outLen, bool freeSectionPalettes){ - char** globalPalette = malloc(0); + char** globalPalette = NULL; int j = 0; for(int i = 0; i < len; i++){ for(int n = 0; n < sections[i].paletteLen; n++){ diff --git a/chunkParser.h b/chunkParser.h index 0875879..76c21b3 100644 --- a/chunkParser.h +++ b/chunkParser.h @@ -3,14 +3,12 @@ //16x16x16 big section of a chunk struct section{ - short y; - //we ignore the biomes because we don't need that data - uint64_t* blockData; //raw nbt file block data - int blockDataLen; //the length of blockData in bytes char** blockPalette; + int blockDataLen; //the length of blockData in bytes int paletteLen; + short y; }; //A block structure with it's own coordinates and a string containing it's type diff --git a/model.c b/model.c index d288bc1..2917b9e 100644 --- a/model.c +++ b/model.c @@ -201,7 +201,7 @@ struct object deCubeObject(struct cube* c){ result.y = objCoordCorrect(c, y, dist); result.z = objCoordCorrect(c, z, dist); result.faceCount = 0; - result.faces = malloc(0); + result.faces = NULL; for(int i = 0; i < 6; i++){ if(c->faces[i] != NULL){ result.faces = realloc(result.faces, (result.faceCount + 1) * sizeof(struct objFace)); @@ -436,6 +436,8 @@ hashTable* getMaterials(char* filename){ hashTable* result = initHashTable(objCount); char* token = strtok(bytes, "\n"); struct material newMaterial; + newMaterial.name = NULL; + newMaterial.d = 0; while(token != NULL){ //foreach line int len = strlen(token); if(len > 7){ @@ -497,6 +499,8 @@ hashTable* readWavefront(char* filename, hashTable* materials, int side){ newObject.x = -1; newObject.y = -1; newObject.z = -1; + newObject.vertexCount = 0; + newObject.faceCount = 0; struct material* nextM = NULL; while(token != NULL){ switch(token[0]){ @@ -520,10 +524,10 @@ hashTable* readWavefront(char* filename, hashTable* materials, int side){ //this should 'reset' the newObject newObject.faceCount = 0; //free(newObject.faces); - newObject.faces = malloc(0); + newObject.faces = NULL; newObject.vertexCount = 0; //free(newObject.vertices); - newObject.vertices = malloc(0); + newObject.vertices = NULL; if(nextM != NULL){ newObject.m = nextM; nextM = NULL; @@ -552,7 +556,7 @@ hashTable* readWavefront(char* filename, hashTable* materials, int side){ newObject.vertexCount++; break; case 'f':; - int* vertices = malloc(0); + int* vertices = NULL; char* num = calloc(1, 1); num[0] = '\0'; int n = 0; diff --git a/radiusGenerator.c b/radiusGenerator.c index 5965c9f..d6909e6 100644 --- a/radiusGenerator.c +++ b/radiusGenerator.c @@ -386,5 +386,6 @@ int main(int argc, char** argv){ FILE* outFile = fopen(outFilename, "w"); fwrite(result, currentSize, 1, outFile); fclose(outFile); + free(result); return EXIT_SUCCESS; } \ No newline at end of file