Jelajahi Sumber

Add warnings and fixes

Kajetan Johannes Hammerle 10 bulan lalu
induk
melakukan
efa834d4e2
2 mengubah file dengan 87 tambahan dan 15 penghapusan
  1. 19 13
      .gitignore
  2. 68 2
      Makefile

+ 19 - 13
.gitignore

@@ -1,17 +1,17 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-char* data = NULL;
+static char* data = NULL;
 
-int hasBit(int index) {
+static int hasBit(size_t index) {
     return (data[index >> 3] & (1 << (index & 7))) == 0;
 }
 
-void setBit(int index) {
-    data[index >> 3] |= (1 << (index & 7));
+static void setBit(size_t index) {
+    data[index >> 3] |= (char)(1 << (index & 7));
 }
 
-int divideRound8(int i) {
+static size_t divideRound8(size_t i) {
     return (i >> 3) + ((i & 7) != 0);
 }
 
@@ -24,27 +24,33 @@ int main(int argAmount, const char** args) {
         }
         return 0;
     }
-    int max = atoi(args[1]);
-    if(max <= 0) {
+    char* endP = NULL;
+    long maxRaw = strtol(args[1], &endP, 10);
+    if(maxRaw <= 0 || *args[1] == '\0' || *endP != '\0') {
         puts("max prime should be a positive number");
         return 0;
     }
+    size_t max = (size_t)maxRaw;
 
-    int end = (max - 1) >> 1;
+    size_t end = (max - 1) >> 1;
     data = calloc(divideRound8(end), 1);
+    if(data == NULL) {
+        puts("failed allocation");
+        return 0;
+    }
 
     int found = 0;
     if(max >= 2) {
         puts("2");
         found++;
     }
-    for(int i = 0; i < end; i++) {
+    for(size_t i = 0; i < end; i++) {
         if(hasBit(i)) {
-            long prime = (i << 1) + 3;
-            for(long k = i + prime; k < end; k += prime) {
+            size_t prime = (i << 1) + 3;
+            for(size_t k = i + prime; k < end; k += prime) {
                 setBit(k);
             }
-            printf("%ld\n", prime);
+            printf("%zu\n", prime);
             found++;
         }
     }
@@ -52,4 +58,4 @@ int main(int argAmount, const char** args) {
 
     free(data);
     return 0;
-}
+}

+ 68 - 2
Makefile

@@ -1,7 +1,73 @@
 all: primes
 	
 primes: Main.c
-	gcc -o $@ Main.c -Wall -Wextra -Werror -pedantic -O3
+	gcc -o $@ Main.c -O3 \
+		-Wall \
+		-Walloc-zero \
+		-Walloca \
+		-Wanalyzer-too-complex \
+		-Warith-conversion \
+		-Warray-bounds=2 \
+		-Warray-parameter \
+		-Wattribute-alias=2 \
+		-Wbad-function-cast \
+		-Wbidi-chars=any \
+		-Wcast-align=strict \
+		-Wcast-qual \
+		-Wconversion \
+		-Wdate-time \
+		-Wdisabled-optimization \
+		-Wdouble-promotion \
+		-Wduplicated-branches \
+		-Wduplicated-cond \
+		-Wenum-compare \
+		-Wenum-conversion \
+		-Werror \
+		-Wextra \
+		-Wfloat-equal \
+		-Wformat-overflow=2 \
+		-Wformat-signedness \
+		-Wformat-truncation=2 \
+		-Wformat=2 \
+		-Wframe-larger-than=8388608 \
+		-Wimplicit-fallthrough=5 \
+		-Winfinite-recursion \
+		-Winit-self \
+		-Winvalid-pch \
+		-Wjump-misses-init \
+		-Wlarger-than=1073741824 \
+		-Wlogical-op \
+		-Wmissing-braces \
+		-Wmissing-declarations \
+		-Wmissing-include-dirs \
+		-Wmissing-prototypes \
+		-Wmultichar \
+		-Wnarrowing \
+		-Wnested-externs \
+		-Wnormalized=nfkc \
+		-Wnull-dereference \
+		-Wold-style-definition \
+		-Woverlength-strings \
+		-Wredundant-decls \
+		-Wshadow \
+		-Wshift-overflow=2 \
+		-Wsign-conversion \
+		-Wstack-protector \
+		-Wstack-usage=8388608 \
+		-Wstrict-overflow=2 \
+		-Wstrict-prototypes \
+		-Wstringop-overflow=4 \
+		-Wswitch-enum \
+		-Wtrampolines \
+		-Wtrivial-auto-var-init \
+		-Wundef \
+		-Wunreachable-code \
+		-Wunused-const-variable=2 \
+		-Wuse-after-free=3 \
+		-Wvla \
+		-Wwrite-strings \
+		-pedantic \
+		-pedantic-errors
 	
 clean:
-	rm -f primes
+	rm -f primes