• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From 409594639f15d825202971db7a275023e05772ff Mon Sep 17 00:00:00 2001
2From: Adenilson Cavalcanti <adenilson.cavalcanti@arm.com>
3Date: Tue, 28 Apr 2020 10:48:01 -0700
4Subject: [PATCH] Local Changes:   - make C tests build as C++ code so we can
5 use gtest.   - use gtest EXPECT_TRUE instead of C assert.   - replace C
6 streams for C++ (portability issues).
7
8---
9 test/infcover.c | 167 ++++++++++++++++++++++++++----------------------
10 1 file changed, 90 insertions(+), 77 deletions(-)
11
12diff --git a/test/infcover.c b/test/infcover.c
13index 2be0164..a8c51c7 100644
14--- a/test/infcover.c
15+++ b/test/infcover.c
16@@ -4,11 +4,12 @@
17  */
18
19 /* to use, do: ./configure --cover && make cover */
20-
21+// clang-format off
22+#include "infcover.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26-#include <assert.h>
27+
28 #include "zlib.h"
29
30 /* get definition of internal structure so we can mess with it (see pull()),
31@@ -17,8 +18,22 @@
32 #include "inftrees.h"
33 #include "inflate.h"
34
35+/* XXX: use C++ streams instead of printf/fputs/etc due to portability
36+ * as type sizes can vary between platforms.
37+ */
38+#include <iostream>
39 #define local static
40
41+/* XXX: hacking C assert and plugging into GTest. */
42+#include "gtest.h"
43+#if defined(assert)
44+#undef assert
45+#define assert EXPECT_TRUE
46+#endif
47+
48+/* XXX: handle what is a reserved word in C++. */
49+#define try try_f
50+
51 /* -- memory tracking routines -- */
52
53 /*
54@@ -72,7 +87,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
55 {
56     void *ptr;
57     struct mem_item *item;
58-    struct mem_zone *zone = mem;
59+    struct mem_zone *zone = static_cast<struct mem_zone *>(mem);
60     size_t len = count * (size_t)size;
61
62     /* induced allocation failure */
63@@ -87,7 +102,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
64     memset(ptr, 0xa5, len);
65
66     /* create a new item for the list */
67-    item = malloc(sizeof(struct mem_item));
68+    item = static_cast<struct mem_item *>(malloc(sizeof(struct mem_item)));
69     if (item == NULL) {
70         free(ptr);
71         return NULL;
72@@ -112,7 +127,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
73 local void mem_free(void *mem, void *ptr)
74 {
75     struct mem_item *item, *next;
76-    struct mem_zone *zone = mem;
77+    struct mem_zone *zone = static_cast<struct mem_zone *>(mem);
78
79     /* if no zone, just do a free */
80     if (zone == NULL) {
81@@ -159,7 +174,7 @@ local void mem_setup(z_stream *strm)
82 {
83     struct mem_zone *zone;
84
85-    zone = malloc(sizeof(struct mem_zone));
86+    zone = static_cast<struct mem_zone *>(malloc(sizeof(struct mem_zone)));
87     assert(zone != NULL);
88     zone->first = NULL;
89     zone->total = 0;
90@@ -175,33 +190,33 @@ local void mem_setup(z_stream *strm)
91 /* set a limit on the total memory allocation, or 0 to remove the limit */
92 local void mem_limit(z_stream *strm, size_t limit)
93 {
94-    struct mem_zone *zone = strm->opaque;
95+    struct mem_zone *zone = static_cast<struct mem_zone *>(strm->opaque);
96
97     zone->limit = limit;
98 }
99
100 /* show the current total requested allocations in bytes */
101-local void mem_used(z_stream *strm, char *prefix)
102+local void mem_used(z_stream *strm, const char *prefix)
103 {
104-    struct mem_zone *zone = strm->opaque;
105+    struct mem_zone *zone = static_cast<struct mem_zone *>(strm->opaque);
106
107-    fprintf(stderr, "%s: %lu allocated\n", prefix, zone->total);
108+    std::cout << prefix << ": " << zone->total << " allocated" << std::endl;
109 }
110
111 /* show the high water allocation in bytes */
112-local void mem_high(z_stream *strm, char *prefix)
113+local void mem_high(z_stream *strm, const char *prefix)
114 {
115-    struct mem_zone *zone = strm->opaque;
116+    struct mem_zone *zone = static_cast<struct mem_zone *>(strm->opaque);
117
118-    fprintf(stderr, "%s: %lu high water mark\n", prefix, zone->highwater);
119+    std::cout << prefix << ": " << zone->highwater << " high water mark" << std::endl;
120 }
121
122 /* release the memory allocation zone -- if there are any surprises, notify */
123-local void mem_done(z_stream *strm, char *prefix)
124+local void mem_done(z_stream *strm, const char *prefix)
125 {
126     int count = 0;
127     struct mem_item *item, *next;
128-    struct mem_zone *zone = strm->opaque;
129+    struct mem_zone *zone = static_cast<struct mem_zone *>(strm->opaque);
130
131     /* show high water mark */
132     mem_high(strm, prefix);
133@@ -218,13 +233,20 @@ local void mem_done(z_stream *strm, char *prefix)
134
135     /* issue alerts about anything unexpected */
136     if (count || zone->total)
137-        fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n",
138-                prefix, zone->total, count);
139+        std::cout << "** " << prefix << ": "
140+                  << zone->total << " bytes in "
141+                  << count << " blocks not freed"
142+                  << std::endl;
143+
144     if (zone->notlifo)
145-        fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo);
146+        std::cout << "** " << prefix << ": "
147+                  << zone->notlifo << " frees not LIFO"
148+                  << std::endl;
149+
150     if (zone->rogue)
151-        fprintf(stderr, "** %s: %d frees not recognized\n",
152-                prefix, zone->rogue);
153+        std::cout << "** " << prefix << ": "
154+                  << zone->rogue << " frees not recognized"
155+                  << std::endl;
156
157     /* free the zone and delete from the stream */
158     free(zone);
159@@ -247,7 +269,7 @@ local unsigned char *h2b(const char *hex, unsigned *len)
160     unsigned char *in, *re;
161     unsigned next, val;
162
163-    in = malloc((strlen(hex) + 1) >> 1);
164+    in = static_cast<unsigned char *>(malloc((strlen(hex) + 1) >> 1));
165     if (in == NULL)
166         return NULL;
167     next = 0;
168@@ -268,7 +290,7 @@ local unsigned char *h2b(const char *hex, unsigned *len)
169     } while (*hex++);       /* go through the loop with the terminating null */
170     if (len != NULL)
171         *len = next;
172-    re = realloc(in, next);
173+    re = static_cast<unsigned char *>(realloc(in, next));
174     return re == NULL ? in : re;
175 }
176
177@@ -281,7 +303,7 @@ local unsigned char *h2b(const char *hex, unsigned *len)
178    header information is collected with inflateGetHeader().  If a zlib stream
179    is looking for a dictionary, then an empty dictionary is provided.
180    inflate() is run until all of the input data is consumed. */
181-local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
182+local void inf(const char *hex, const char *what, unsigned step, int win, unsigned len,
183                int err)
184 {
185     int ret;
186@@ -298,7 +320,7 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
187         mem_done(&strm, what);
188         return;
189     }
190-    out = malloc(len);                          assert(out != NULL);
191+    out = static_cast<unsigned char *>(malloc(len));                          assert(out != NULL);
192     if (win == 47) {
193         head.extra = out;
194         head.extra_max = len;
195@@ -347,7 +369,7 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
196 }
197
198 /* cover all of the lines in inflate.c up to inflate() */
199-local void cover_support(void)
200+void cover_support(void)
201 {
202     int ret;
203     z_stream strm;
204@@ -381,11 +403,11 @@ local void cover_support(void)
205     strm.next_in = Z_NULL;
206     ret = inflateInit(&strm);                   assert(ret == Z_OK);
207     ret = inflateEnd(&strm);                    assert(ret == Z_OK);
208-    fputs("inflate built-in memory routines\n", stderr);
209+    std::cout << "inflate built-in memory routines" << std::endl;;
210 }
211
212 /* cover all inflate() header and trailer cases and code after inflate() */
213-local void cover_wrap(void)
214+void cover_wrap(void)
215 {
216     int ret;
217     z_stream strm, copy;
218@@ -394,7 +416,7 @@ local void cover_wrap(void)
219     ret = inflate(Z_NULL, 0);                   assert(ret == Z_STREAM_ERROR);
220     ret = inflateEnd(Z_NULL);                   assert(ret == Z_STREAM_ERROR);
221     ret = inflateCopy(Z_NULL, Z_NULL);          assert(ret == Z_STREAM_ERROR);
222-    fputs("inflate bad parameters\n", stderr);
223+    std::cout << "inflate bad parameters" << std::endl;
224
225     inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR);
226     inf("1f 8b 8 80", "bad gzip flags", 0, 31, 0, Z_DATA_ERROR);
227@@ -415,9 +437,9 @@ local void cover_wrap(void)
228     strm.next_in = Z_NULL;
229     ret = inflateInit2(&strm, -8);
230     strm.avail_in = 2;
231-    strm.next_in = (void *)"\x63";
232+    strm.next_in = (Bytef *)"\x63";
233     strm.avail_out = 1;
234-    strm.next_out = (void *)&ret;
235+    strm.next_out = (Bytef *)&ret;
236     mem_limit(&strm, 1);
237     ret = inflate(&strm, Z_NO_FLUSH);           assert(ret == Z_MEM_ERROR);
238     ret = inflate(&strm, Z_NO_FLUSH);           assert(ret == Z_MEM_ERROR);
239@@ -428,11 +450,11 @@ local void cover_wrap(void)
240     mem_limit(&strm, (sizeof(struct inflate_state) << 1) + 256);
241     ret = inflatePrime(&strm, 16, 0);           assert(ret == Z_OK);
242     strm.avail_in = 2;
243-    strm.next_in = (void *)"\x80";
244+    strm.next_in = (Bytef *)"\x80";
245     ret = inflateSync(&strm);                   assert(ret == Z_DATA_ERROR);
246     ret = inflate(&strm, Z_NO_FLUSH);           assert(ret == Z_STREAM_ERROR);
247     strm.avail_in = 4;
248-    strm.next_in = (void *)"\0\0\xff\xff";
249+    strm.next_in = (Bytef *)"\0\0\xff\xff";
250     ret = inflateSync(&strm);                   assert(ret == Z_OK);
251     (void)inflateSyncPoint(&strm);
252     ret = inflateCopy(&copy, &strm);            assert(ret == Z_MEM_ERROR);
253@@ -454,7 +476,7 @@ local unsigned pull(void *desc, unsigned char **buf)
254         next = 0;
255         return 0;   /* no input (already provided at next_in) */
256     }
257-    state = (void *)((z_stream *)desc)->state;
258+    state = reinterpret_cast<struct inflate_state *>(((z_stream *)desc)->state);
259     if (state != Z_NULL)
260         state->mode = SYNC;     /* force an otherwise impossible situation */
261     return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
262@@ -467,7 +489,7 @@ local int push(void *desc, unsigned char *buf, unsigned len)
263 }
264
265 /* cover inflateBack() up to common deflate data cases and after those */
266-local void cover_back(void)
267+void cover_back(void)
268 {
269     int ret;
270     z_stream strm;
271@@ -479,17 +501,17 @@ local void cover_back(void)
272     ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL);
273                                                 assert(ret == Z_STREAM_ERROR);
274     ret = inflateBackEnd(Z_NULL);               assert(ret == Z_STREAM_ERROR);
275-    fputs("inflateBack bad parameters\n", stderr);
276+    std::cout << "inflateBack bad parameters" << std::endl;;
277
278     mem_setup(&strm);
279     ret = inflateBackInit(&strm, 15, win);      assert(ret == Z_OK);
280     strm.avail_in = 2;
281-    strm.next_in = (void *)"\x03";
282+    strm.next_in = (Bytef *)"\x03";
283     ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
284                                                 assert(ret == Z_STREAM_END);
285         /* force output error */
286     strm.avail_in = 3;
287-    strm.next_in = (void *)"\x63\x00";
288+    strm.next_in = (Bytef *)"\x63\x00";
289     ret = inflateBack(&strm, pull, Z_NULL, push, &strm);
290                                                 assert(ret == Z_BUF_ERROR);
291         /* force mode error by mucking with state */
292@@ -500,11 +522,11 @@ local void cover_back(void)
293
294     ret = inflateBackInit(&strm, 15, win);      assert(ret == Z_OK);
295     ret = inflateBackEnd(&strm);                assert(ret == Z_OK);
296-    fputs("inflateBack built-in memory routines\n", stderr);
297+    std::cout << "inflateBack built-in memory routines" << std::endl;;
298 }
299
300 /* do a raw inflate of data in hexadecimal with both inflate and inflateBack */
301-local int try(char *hex, char *id, int err)
302+local int try(const char *hex, const char *id, int err)
303 {
304     int ret;
305     unsigned len, size;
306@@ -518,11 +540,11 @@ local int try(char *hex, char *id, int err)
307
308     /* allocate work areas */
309     size = len << 3;
310-    out = malloc(size);
311+    out = static_cast<unsigned char *>(malloc(size));
312     assert(out != NULL);
313-    win = malloc(32768);
314+    win = static_cast<unsigned char *>(malloc(32768));
315     assert(win != NULL);
316-    prefix = malloc(strlen(id) + 6);
317+    prefix = static_cast<char *>(malloc(strlen(id) + 6));
318     assert(prefix != NULL);
319
320     /* first with inflate */
321@@ -578,7 +600,7 @@ local int try(char *hex, char *id, int err)
322 }
323
324 /* cover deflate data cases in both inflate() and inflateBack() */
325-local void cover_inflate(void)
326+void cover_inflate(void)
327 {
328     try("0 0 0 0 0", "invalid stored block lengths", 1);
329     try("3 0", "fixed", 0);
330@@ -613,32 +635,33 @@ local void cover_inflate(void)
331     inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK);
332 }
333
334+/* XXX(cavalcantii): fix linking error due inflate_table. */
335 /* cover remaining lines in inftrees.c */
336-local void cover_trees(void)
337-{
338-    int ret;
339-    unsigned bits;
340-    unsigned short lens[16], work[16];
341-    code *next, table[ENOUGH_DISTS];
342-
343-    /* we need to call inflate_table() directly in order to manifest not-
344-       enough errors, since zlib insures that enough is always enough */
345-    for (bits = 0; bits < 15; bits++)
346-        lens[bits] = (unsigned short)(bits + 1);
347-    lens[15] = 15;
348-    next = table;
349-    bits = 15;
350-    ret = inflate_table(DISTS, lens, 16, &next, &bits, work);
351-                                                assert(ret == 1);
352-    next = table;
353-    bits = 1;
354-    ret = inflate_table(DISTS, lens, 16, &next, &bits, work);
355-                                                assert(ret == 1);
356-    fputs("inflate_table not enough errors\n", stderr);
357-}
358+/* void cover_trees(void) */
359+/* { */
360+/*     int ret; */
361+/*     unsigned bits; */
362+/*     unsigned short lens[16], work[16]; */
363+/*     code *next, table[ENOUGH_DISTS]; */
364+
365+/*     /\* we need to call inflate_table() directly in order to manifest not- */
366+/*        enough errors, since zlib insures that enough is always enough *\/ */
367+/*     for (bits = 0; bits < 15; bits++) */
368+/*         lens[bits] = (unsigned short)(bits + 1); */
369+/*     lens[15] = 15; */
370+/*     next = table; */
371+/*     bits = 15; */
372+/*     ret = inflate_table(DISTS, lens, 16, &next, &bits, work); */
373+/*                                                 assert(ret == 1); */
374+/*     next = table; */
375+/*     bits = 1; */
376+/*     ret = inflate_table(DISTS, lens, 16, &next, &bits, work); */
377+/*                                                 assert(ret == 1); */
378+/*     fputs("inflate_table not enough errors\n", stderr); */
379+/* } */
380
381 /* cover remaining inffast.c decoding and window copying */
382-local void cover_fast(void)
383+void cover_fast(void)
384 {
385     inf("e5 e0 81 ad 6d cb b2 2c c9 01 1e 59 63 ae 7d ee fb 4d fd b5 35 41 68"
386         " ff 7f 0f 0 0 0", "fast length extra bits", 0, -8, 258, Z_DATA_ERROR);
387@@ -658,14 +681,4 @@ local void cover_fast(void)
388         Z_STREAM_END);
389 }
390
391-int main(void)
392-{
393-    fprintf(stderr, "%s\n", zlibVersion());
394-    cover_support();
395-    cover_wrap();
396-    cover_back();
397-    cover_inflate();
398-    cover_trees();
399-    cover_fast();
400-    return 0;
401-}
402+// clang-format on
403--
4042.21.1 (Apple Git-122.3)
405
406