Selaa lähdekoodia

static analysis

Thomas Buck 10 vuotta sitten
vanhempi
commit
60300a9418
9 muutettua tiedostoa jossa 85 lisäystä ja 41 poistoa
  1. 2
    0
      ChangeLog
  2. 8
    0
      Makefile
  3. 6
    6
      src/MatMath.cpp
  4. 1
    1
      src/Particle.cpp
  5. 8
    1
      src/Texture.cpp
  6. 1
    1
      src/TombRaider.cpp
  7. 2
    2
      src/memory_test.cpp
  8. 15
    15
      test/greatest.h
  9. 42
    15
      test/memory_test.cpp

+ 2
- 0
ChangeLog Näytä tiedosto

@@ -7,6 +7,8 @@
7 7
 
8 8
 	[ 20140201 ]
9 9
 	* Rewrote Memory Unit Test using greatest
10
+	* Used C++ static analysis tool cppcheck and tried to fix
11
+	  its warnings
10 12
 
11 13
 	[ 20140131 ]
12 14
 	* All unit tests buildable again and no more warnings

+ 8
- 0
Makefile Näytä tiedosto

@@ -205,6 +205,14 @@ $(BUILDDIR)/$(NAME) : $(OBJS)
205 205
 
206 206
 #################################################################
207 207
 
208
+check:
209
+	cppcheck -Iinclude $(GL_DEFS) $(AUDIO_DEFS) --enable=information .
210
+
211
+fullCheck:
212
+	cppcheck -Iinclude $(GL_DEFS) $(AUDIO_DEFS) --enable=all --inconclusive --std=posix .
213
+
214
+#################################################################
215
+
208 216
 clean: clean-small clean-dep clean-doc
209 217
 
210 218
 clean-small: clean-build clean-test clean-obj 

+ 6
- 6
src/MatMath.cpp Näytä tiedosto

@@ -77,9 +77,9 @@ int helIntersectionOfAbstractSphereAndLine(vec3_t center, vec_t radius,
77 77
     {
78 78
         // One intersection
79 79
         mu = -b/(2*a) ;
80
-        intersectionA[1] = posA[0] + mu*(posB[0]-posA[0]);
81
-        intersectionA[2] = posA[1] + mu*(posB[1]-posA[1]);
82
-        intersectionA[3] = posA[2] + mu*(posB[2]-posA[2]);
80
+        intersectionA[0] = posA[0] + mu*(posB[0]-posA[0]);
81
+        intersectionA[1] = posA[1] + mu*(posB[1]-posA[1]);
82
+        intersectionA[2] = posA[2] + mu*(posB[2]-posA[2]);
83 83
 
84 84
         return 1;
85 85
     }
@@ -89,9 +89,9 @@ int helIntersectionOfAbstractSphereAndLine(vec3_t center, vec_t radius,
89 89
 
90 90
         // First intersection
91 91
         mu = (-b + sqrtf( square(b) - 4.0f*a*c)) / (2.0f*a);
92
-        intersectionA[1] = posA[0] + mu*(posB[0]-posA[0]);
93
-        intersectionA[2] = posA[1] + mu*(posB[1]-posA[1]);
94
-        intersectionA[3] = posA[2] + mu*(posB[2]-posA[2]);
92
+        intersectionA[0] = posA[0] + mu*(posB[0]-posA[0]);
93
+        intersectionA[1] = posA[1] + mu*(posB[1]-posA[1]);
94
+        intersectionA[2] = posA[2] + mu*(posB[2]-posA[2]);
95 95
 
96 96
         // Second intersection
97 97
         mu = (-b - sqrtf(square(b) - 4.0f*a*c)) / (2.0f*a);

+ 1
- 1
src/Particle.cpp Näytä tiedosto

@@ -71,7 +71,7 @@ void Particle::Force(float x, float y, float z)
71 71
 void Particle::Reset()
72 72
 {
73 73
     // Mongoose 2002.01.01, Ah, how old is that code?
74
-#ifdef OBSOLOETE
74
+#ifdef OBSOLETE
75 75
     _active = true;
76 76
     _life = 1.0;
77 77
     _blend = (float)(rand() % 100) / 1000.0 + 0.003;

+ 8
- 1
src/Texture.cpp Näytä tiedosto

@@ -846,6 +846,7 @@ void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
846 846
         {
847 847
             delete [] image;
848 848
         }
849
+        delete [] swap_row;
849 850
 
850 851
         printf("glScreenShot> ERROR: Couldn't allocate image!\n");
851 852
         return;
@@ -870,6 +871,8 @@ void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
870 871
     {
871 872
         printf("glScreenShot> ERROR: Couldn't write screenshot.\n");
872 873
         perror("glScreenShot> ERROR: ");
874
+        delete [] image;
875
+        delete [] swap_row;
873 876
         return;
874 877
     }
875 878
 
@@ -929,6 +932,7 @@ void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
929 932
     {
930 933
         perror("glScreenShot> Disk write failed.\n");
931 934
         fclose(f);
935
+        delete [] image;
932 936
         return;
933 937
     }
934 938
 
@@ -1050,7 +1054,7 @@ unsigned char *Texture::scaleBuffer(unsigned char *image,
1050 1054
     tempin = new float[original_width * original_height * components * sizeof(float)];
1051 1055
     tempout = new float[width * height * components * sizeof(float)];
1052 1056
 
1053
-    if (!tempout || !tempin)
1057
+    if (!tempout || !tempin || !timage)
1054 1058
     {
1055 1059
         if (tempout)
1056 1060
             delete [] tempout;
@@ -1058,6 +1062,9 @@ unsigned char *Texture::scaleBuffer(unsigned char *image,
1058 1062
         if (tempin)
1059 1063
             delete [] tempin;
1060 1064
 
1065
+        if (timage)
1066
+            delete [] timage;
1067
+
1061 1068
         printf("Oh shit out of memory!\n");
1062 1069
         return NULL;
1063 1070
     }

+ 1
- 1
src/TombRaider.cpp Näytä tiedosto

@@ -3923,7 +3923,7 @@ void TombRaider::getSprites()
3923 3923
     sprite_seq_t *r_mesh;
3924 3924
     tr2_item_t *item;
3925 3925
 
3926
-
3926
+    r_mesh = Mesh();
3927 3927
     item = Item();
3928 3928
     sprite_textures = Sprite();
3929 3929
     sprite_sequence = SpriteSequence();

+ 2
- 2
src/memory_test.cpp Näytä tiedosto

@@ -426,8 +426,8 @@ int tree_print(rbtree_t *tree, void (*print_func)(void *))
426 426
 
427 427
 #ifdef DEBUG_MEMORY_RBTREE
428 428
         printf(" :%s%s)",
429
-                (!tree->parent) ? " (root, " : " ("),
430
-            (tree->color == RB_BLACK) ? "black" : "red");
429
+                ((!tree->parent) ? " (root, " : " ("),
430
+            ((tree->color == RB_BLACK) ? "black" : "red"));
431 431
 #endif
432 432
     }
433 433
 

+ 15
- 15
test/greatest.h Näytä tiedosto

@@ -510,38 +510,38 @@ greatest_run_info greatest_info
510 510
 /* Handle command-line arguments, etc. */
511 511
 #define GREATEST_MAIN_BEGIN()                                           \
512 512
     do {                                                                \
513
-        int i = 0;                                                      \
513
+        int _i = 0;                                                      \
514 514
         memset(&greatest_info, 0, sizeof(greatest_info));               \
515 515
         if (greatest_info.width == 0) {                                 \
516 516
             greatest_info.width = GREATEST_DEFAULT_WIDTH;               \
517 517
         }                                                               \
518
-        for (i = 1; i < argc; i++) {                                    \
519
-            if (0 == strcmp("-t", argv[i])) {                           \
520
-                if (argc <= i + 1) {                                    \
518
+        for (_i = 1; _i < argc; _i++) {                                    \
519
+            if (0 == strcmp("-t", argv[_i])) {                           \
520
+                if (argc <= _i + 1) {                                    \
521 521
                     greatest_usage(argv[0]);                            \
522 522
                     exit(EXIT_FAILURE);                                 \
523 523
                 }                                                       \
524
-                greatest_info.test_filter = argv[i+1];                  \
525
-                i++;                                                    \
526
-            } else if (0 == strcmp("-s", argv[i])) {                    \
527
-                if (argc <= i + 1) {                                    \
524
+                greatest_info.test_filter = argv[_i+1];                  \
525
+                _i++;                                                    \
526
+            } else if (0 == strcmp("-s", argv[_i])) {                    \
527
+                if (argc <= _i + 1) {                                    \
528 528
                     greatest_usage(argv[0]);                            \
529 529
                     exit(EXIT_FAILURE);                                 \
530 530
                 }                                                       \
531
-                greatest_info.suite_filter = argv[i+1];                 \
532
-                i++;                                                    \
533
-            } else if (0 == strcmp("-f", argv[i])) {                    \
531
+                greatest_info.suite_filter = argv[_i+1];                 \
532
+                _i++;                                                    \
533
+            } else if (0 == strcmp("-f", argv[_i])) {                    \
534 534
                 greatest_info.flags |= GREATEST_FLAG_FIRST_FAIL;        \
535
-            } else if (0 == strcmp("-v", argv[i])) {                    \
535
+            } else if (0 == strcmp("-v", argv[_i])) {                    \
536 536
                 greatest_info.flags |= GREATEST_FLAG_VERBOSE;           \
537
-            } else if (0 == strcmp("-l", argv[i])) {                    \
537
+            } else if (0 == strcmp("-l", argv[_i])) {                    \
538 538
                 greatest_info.flags |= GREATEST_FLAG_LIST_ONLY;         \
539
-            } else if (0 == strcmp("-h", argv[i])) {                    \
539
+            } else if (0 == strcmp("-h", argv[_i])) {                    \
540 540
                 greatest_usage(argv[0]);                                \
541 541
                 exit(EXIT_SUCCESS);                                     \
542 542
             } else {                                                    \
543 543
                 fprintf(GREATEST_STDOUT,                                \
544
-                    "Unknown argument '%s'\n", argv[i]);                \
544
+                    "Unknown argument '%s'\n", argv[_i]);                \
545 545
                 greatest_usage(argv[0]);                                \
546 546
                 exit(EXIT_FAILURE);                                     \
547 547
             }                                                           \

+ 42
- 15
test/memory_test.cpp Näytä tiedosto

@@ -15,87 +15,114 @@
15 15
 extern rbtree_t *MEMORY_INFO;
16 16
 bool check_red_black_tree(rbtree_t *current, bool valid, bool strict);
17 17
 
18
+int *i, *j, *k;
19
+float *l, *m, *n;
20
+
21
+void integerTeardown(void *) {
22
+    free(i);
23
+    free(j);
24
+    free(k);
25
+}
26
+
27
+void floatTeardown(void *) {
28
+    free(l);
29
+    free(m);
30
+    free(n);
31
+}
32
+
18 33
 TEST singleInteger() {
19
-    int *i = new int;
34
+    i = new int;
20 35
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
21 36
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), sizeof(int));
22 37
 
23 38
     delete i;
39
+    i = NULL;
24 40
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
25 41
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
26 42
     PASS();
27 43
 }
28 44
 
29 45
 TEST arrayInteger() {
30
-    int *i = new int[3];
46
+    i = new int[3];
31 47
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
32 48
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 3 * sizeof(int));
33 49
 
34 50
     delete [] i;
51
+    i = NULL;
35 52
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
36 53
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
37 54
     PASS();
38 55
 }
39 56
 
40 57
 TEST arrayCombinedInteger() {
41
-    int *i = new int[3];
42
-    int *j = new int;
43
-    int *k = new int[3];
58
+    i = new int[3];
59
+    j = new int;
60
+    k = new int[3];
44 61
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
45 62
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 7 * sizeof(int));
46 63
 
47 64
     delete [] i;
48 65
     delete j;
49 66
     delete [] k;
67
+    i = NULL;
68
+    j = NULL;
69
+    k = NULL;
50 70
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
51 71
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
52 72
     PASS();
53 73
 }
54 74
 
55 75
 SUITE(integerSuite) {
76
+    SET_TEARDOWN(integerTeardown, NULL);
56 77
     RUN_TEST(singleInteger);
57 78
     RUN_TEST(arrayInteger);
58 79
     RUN_TEST(arrayCombinedInteger);
59 80
 }
60 81
 
61 82
 TEST singleFloat() {
62
-    float *i = new float;
83
+    l = new float;
63 84
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
64 85
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), sizeof(float));
65 86
 
66
-    delete i;
87
+    delete l;
88
+    l = NULL;
67 89
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
68 90
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
69 91
     PASS();
70 92
 }
71 93
 
72 94
 TEST arrayFloat() {
73
-    float *i = new float[3];
95
+    l = new float[3];
74 96
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
75 97
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 3 * sizeof(float));
76 98
 
77
-    delete [] i;
99
+    delete [] l;
100
+    l = NULL;
78 101
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
79 102
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
80 103
     PASS();
81 104
 }
82 105
 
83 106
 TEST arrayCombinedFloat() {
84
-    float *i = new float[3];
85
-    float *j = new float;
86
-    float *k = new float[3];
107
+    l = new float[3];
108
+    m = new float;
109
+    n = new float[3];
87 110
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
88 111
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 7 * sizeof(float));
89 112
 
90
-    delete [] i;
91
-    delete j;
92
-    delete [] k;
113
+    delete [] l;
114
+    delete m;
115
+    delete [] n;
116
+    l = NULL;
117
+    m = NULL;
118
+    n = NULL;
93 119
     ASSERTm("No valid red-black tree!", check_red_black_tree(MEMORY_INFO, true, true));
94 120
     ASSERT_EQm("Memory-Tracking faulty!", memory_used(MEMORY_USED_BY_PROGRAM), 0);
95 121
     PASS();
96 122
 }
97 123
 
98 124
 SUITE(floatSuite) {
125
+    SET_TEARDOWN(floatTeardown, NULL);
99 126
     RUN_TEST(singleFloat);
100 127
     RUN_TEST(arrayFloat);
101 128
     RUN_TEST(arrayCombinedFloat);

Loading…
Peruuta
Tallenna