]> andersk Git - splint.git/blame - src/general.c
Fixed state clauses. Added obvious loop exec checking.
[splint.git] / src / general.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
77d37419 3** Copyright (C) 1994-2002 University of Virginia,
616915dd 4** Massachusetts Institute of Technology
5**
6** This program is free software; you can redistribute it and/or modify it
7** under the terms of the GNU General Public License as published by the
8** Free Software Foundation; either version 2 of the License, or (at your
9** option) any later version.
10**
11** This program is distributed in the hope that it will be useful, but
12** WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14** General Public License for more details.
15**
16** The GNU General Public License is available from http://www.gnu.org/ or
17** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18** MA 02111-1307, USA.
19**
20** For information on lclint: lclint-request@cs.virginia.edu
21** To report a bug: lclint-bug@cs.virginia.edu
11db3170 22** For more information: http://www.splint.org
616915dd 23*/
24/*
25** general.c
26*/
27
28# include "lclintMacros.nf"
29# include "basic.h"
30# undef malloc
31# undef realloc
32# undef calloc
33
34# ifdef USEDMALLOC
35# include "dmalloc.h"
36# endif
37
38# include "portab.h"
39
40/*
41** redefine undef'd memory ops
42*/
43
44# ifndef USEDMALLOC
616915dd 45
46/*@-mustdefine@*/
47
48void sfree (void *x)
49{
50 if (x != NULL)
51 {
28bf4b0b 52 /* fprintf (stderr, "Freeing: %p\n", x); */
53
616915dd 54 /*
28bf4b0b 55 if ((unsigned long) x > 0xbf000000) {
616915dd 56 fprintf (stderr, "Looks bad!\n");
57 }
58 */
28bf4b0b 59
616915dd 60 free (x);
28bf4b0b 61
616915dd 62 /* fprintf (stderr, "Done.\n"); */
63 }
64}
65# endif
66
67void sfreeEventually (void *x)
68{
69 if (x != NULL)
70 {
71 ; /* should keep in a table */
72 }
73/*@-mustfree@*/
74} /*@=mustfree@*/
75
76/*
77** all memory should be allocated from dimalloc
78*/
79
80static long unsigned size_toLongUnsigned (size_t x)
81{
82 long unsigned res = (long unsigned) x;
83
84 llassert ((size_t) res == x);
85 return res;
86}
87
28bf4b0b 88/*@out@*/ void *dimalloc (size_t size, const char *name, int line)
616915dd 89{
90 /*
91 static void *lastaddr = 0;
92 static int numallocs = 0;
93 static int numbad = 0;
94 */
95
96 /* was malloc, use calloc to initialize to zero */
97 void *ret = (void *) calloc (1, size);
98
99 /*
100 numallocs++;
101
102 if (ret < lastaddr)
103 {
104 numbad++;
105 fprintf (stderr, "Bad alloc: %d / %d\n", numbad, numallocs);
106 }
107
108 lastaddr = ret;
109 */
110
111 if (ret == NULL)
112 {
113 if (size == 0)
114 {
115 llbug (message ("Zero allocation at %q.",
116 fileloc_unparseRaw (cstring_fromChars (name), line)));
117 }
118 else
119 {
a779b61e 120 /* drl
121 fix this so message doesn't run out of
122 memory*/
123
124 llbuglit("Out of memory");
125
616915dd 126 llfatalerrorLoc
127 (message ("Out of memory. Allocating %w bytes at %s:%d.",
128 size_toLongUnsigned (size),
129 cstring_fromChars (name), line));
a779b61e 130
616915dd 131 }
132 }
133
134 /*@-null@*/ /* null okay for size = 0 */
28bf4b0b 135 /* fprintf (stderr, "%s:%d: Allocating: [%p / %d]\n", name, line, ret, size); */
616915dd 136 return ret;
137 /*@=null@*/
138}
139
28bf4b0b 140void *dicalloc (size_t num, size_t size, const char *name, int line)
616915dd 141{
142 void *ret = (void *) calloc (num, size);
143
144 if (ret == NULL)
145 {
146 llfatalerrorLoc
147 (message ("Out of memory. Allocating %w bytes at %s:%d.",
148 size_toLongUnsigned (size),
149 cstring_fromChars (name), line));
150 }
151
152 return ret;
153}
154
155void *direalloc (/*@out@*/ /*@null@*/ void *x, size_t size,
156 char *name, int line)
157{
158 void *ret;
159
160 if (x == NULL)
161 {
162 ret = (void *) dmalloc (size);
163 }
164 else
165 {
166 ret = (void *) realloc (x, size);
167 }
168
169 if (ret == NULL)
170 {
171 llfatalerrorLoc
172 (message ("Out of memory. Allocating %w bytes at %s:%d.",
173 size_toLongUnsigned (size),
174 cstring_fromChars (name), line));
175 }
176
177 return ret;
178}
179
180/*@=mustdefine@*/
181
182# ifndef NOLCL
183char *FormatInt (int i)
184{
185 char temp[255]; /* assume the integer has at most 254 digits */
186 char *outs;
187
188 sprintf (temp, "%i", i);
189 outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
190 strcpy (outs, temp);
191
192 return (outs);
193}
194# endif
195
616915dd 196# ifndef NOLCL
197bool firstWord (char *s, char *w)
198{
199 llassert (s != NULL);
200 llassert (w != NULL);
201
202 for (; *w != '\0'; w++, s++)
203 {
204 if (*w != *s || *s == '\0')
205 return FALSE;
206 }
207 return TRUE;
208}
209# endif
210
616915dd 211void mstring_markFree (char *s)
212{
15b3d2b2 213 sfreeEventually (s);
616915dd 214}
215
616915dd 216char *mstring_spaces (int n)
217{
218 int i;
219 char *ret;
220 char *ptr;
221
222 llassert (n >= 0);
223
224 ret = (char *) dmalloc (size_fromInt (n + 1));
225 ptr = ret;
226
227 for (i = 0; i < n; i++)
228 {
229 *ptr++ = ' ';
230 }
231
232 *ptr = '\0';
233
234 return ret;
235}
28bf4b0b 236
237bool mstring_containsChar (const char *s, char c)
238{
239 if (mstring_isDefined (s))
240 {
241 return (strchr (s, c) != NULL);
242 }
243 else
244 {
245 return FALSE;
246 }
247}
68de3f33 248
249bool mstring_containsString (const char *s, const char *c)
250{
251 if (mstring_isDefined (s))
252 {
253 return (strstr (s, c) != NULL);
254 }
255 else
256 {
257 return FALSE;
258 }
259}
616915dd 260
261char *mstring_concat (const char *s1, const char *s2)
262{
263 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
264 strcpy (s, s1);
265 strcat (s, s2);
266 return s;
267}
268
269extern /*@only@*/ char *
270mstring_concatFree (/*@only@*/ char *s1, /*@only@*/ char *s2)
271{
272 /* like mstring_concat but deallocates old strings */
273 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
274 strcpy (s, s1);
275 strcat (s, s2);
276
277 sfree (s1);
278 sfree (s2);
279 return s;
280}
281
282extern /*@only@*/ char *
283mstring_concatFree1 (/*@only@*/ char *s1, const char *s2)
284{
285 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
286 strcpy (s, s1);
287 strcat (s, s2);
288 sfree (s1);
289
290 return s;
291}
292
293extern /*@only@*/ char *
294mstring_append (/*@only@*/ char *s1, char c)
295{
296 size_t l = strlen (s1);
297 char *s;
298
299 s = (char *) dmalloc (sizeof (*s) * (l + 2));
300
301 strcpy (s, s1);
302 *(s + l) = c;
303 *(s + l + 1) = '\0';
304 sfree (s1);
305 return s;
306}
307
308extern
309char *mstring_copy (char *s1)
310{
311 if (s1 == NULL)
312 {
313 return NULL;
314 }
315 else
316 {
317 char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
318 strcpy (s, s1);
319 return s;
320 }
321}
322
323extern
324char *mstring_safePrint (char *s)
325{
326 if (s == NULL)
327 {
328 return ("<undefined>");
329 }
330 else
331 {
332 return s;
333 }
334}
335
336extern
337char *mstring_create (int n)
338{
339 char *s;
340
341 s = dmalloc (sizeof (*s) * (n + 1));
342 *s = '\0';
343 return s;
344}
345
346void
347fputline (FILE *out, char *s)
348{
349 if (strlen (s) > 0)
350 {
351 check (fputs (s, out) != EOF);
352 }
353
354 check (fputc ('\n', out) == (int) '\n');
355}
356
357int int_log (int x)
358{
359 int ret = 1;
360
361 while (x > 10)
362 {
363 ret++;
364 x /= 10;
365 }
366
367 return ret;
368}
369
370/*@-czechfcns@*/
371long unsigned int
372longUnsigned_fromInt (int x)
373{
374 llassert (x >= 0);
375
376 return (long unsigned) x;
377}
378
379size_t size_fromInt (int x)
380{
381 size_t res = (size_t) x;
382
383 llassert ((int) res == x);
384 return res;
385}
386
387int size_toInt (size_t x)
388{
389 int res = (int) x;
390
391 llassert ((size_t) res == x);
392 return res;
393}
394
395long size_toLong (size_t x)
396{
397 long res = (long) x;
398
399 llassert ((size_t) res == x);
400 return res;
401}
402
403/*@=czechfcns@*/
404
405char
406char_fromInt (int x)
407{
2209bcb7 408 /*
409 ** evans 2001-09-28 - changed assertion in response to Anthony Giorgio's comment
410 ** that the old assertions failed for EBCDIC character set. Now we just check
411 ** that the result is equal.
412 */
616915dd 413
2209bcb7 414 char res = (char) x;
415 llassert ((int) res == x);
416 return res;
616915dd 417}
418
419/*@-czechfcns@*/
420int
421longUnsigned_toInt (long unsigned int x)
422{
423 int res = (int) x;
424
425 llassert ((long unsigned) res == x);
426 return res;
427}
428
429int
430long_toInt (long int x)
431{
432 int res = (int) x;
433
434 /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
435 return res;
436}
437
438/*@+czechfcns@*/
439
440bool mstring_equalPrefix (const char *c1, const char *c2)
441{
442 llassert (c1 != NULL);
443 llassert (c2 != NULL);
444
445 if (strncmp(c1, c2, strlen(c2)) == 0)
446 {
447 return TRUE;
448 }
449 else
450 {
451 return FALSE;
452 }
453}
454
455bool mstring_equal (/*@null@*/ const char *s1, /*@null@*/ const char *s2)
456{
457 if (s1 == NULL)
458 {
459 return (s2 == NULL);
460 }
461 else
462 {
463 if (s2 == NULL)
464 {
465 return FALSE;
466 }
467 else
468 {
469 return (strcmp(s1, s2) == 0);
470 }
471 }
472}
473
This page took 0.11135 seconds and 5 git commands to generate.