2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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.
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
28 # include "lclintMacros.nf"
41 ** redefine undef'd memory ops
52 /* fprintf (stderr, "Freeing: %p\n", x); */
55 if ((unsigned long) x > 0xbf000000) {
56 fprintf (stderr, "Looks bad!\n");
62 /* fprintf (stderr, "Done.\n"); */
67 void sfreeEventually (void *x)
71 ; /* should keep in a table */
77 ** all memory should be allocated from dimalloc
80 static long unsigned size_toLongUnsigned (size_t x)
82 long unsigned res = (long unsigned) x;
84 llassert ((size_t) res == x);
88 /*@out@*/ void *dimalloc (size_t size, const char *name, int line)
91 static void *lastaddr = 0;
92 static int numallocs = 0;
93 static int numbad = 0;
96 /* was malloc, use calloc to initialize to zero */
97 void *ret = (void *) calloc (1, size);
105 fprintf (stderr, "Bad alloc: %d / %d\n", numbad, numallocs);
115 llbug (message ("Zero allocation at %q.",
116 fileloc_unparseRaw (cstring_fromChars (name), line)));
121 fix this so message doesn't run out of
124 llbuglit("Out of memory");
127 (message ("Out of memory. Allocating %w bytes at %s:%d.",
128 size_toLongUnsigned (size),
129 cstring_fromChars (name), line));
134 /*@-null@*/ /* null okay for size = 0 */
135 /* fprintf (stderr, "%s:%d: Allocating: [%p / %d]\n", name, line, ret, size); */
140 void *dicalloc (size_t num, size_t size, const char *name, int line)
142 void *ret = (void *) calloc (num, size);
147 (message ("Out of memory. Allocating %w bytes at %s:%d.",
148 size_toLongUnsigned (size),
149 cstring_fromChars (name), line));
155 void *direalloc (/*@out@*/ /*@null@*/ void *x, size_t size,
156 char *name, int line)
162 ret = (void *) dmalloc (size);
166 ret = (void *) realloc (x, size);
172 (message ("Out of memory. Allocating %w bytes at %s:%d.",
173 size_toLongUnsigned (size),
174 cstring_fromChars (name), line));
183 char *FormatInt (int i)
185 char temp[255]; /* assume the integer has at most 254 digits */
188 sprintf (temp, "%i", i);
189 outs = (char *) dmalloc (sizeof (*outs) * (1 + strlen (temp)));
197 bool firstWord (char *s, char *w)
199 llassert (s != NULL);
200 llassert (w != NULL);
202 for (; *w != '\0'; w++, s++)
204 if (*w != *s || *s == '\0')
211 void mstring_markFree (char *s)
216 char *mstring_spaces (int n)
224 ret = (char *) dmalloc (size_fromInt (n + 1));
227 for (i = 0; i < n; i++)
237 bool mstring_containsChar (const char *s, char c)
239 if (mstring_isDefined (s))
241 return (strchr (s, c) != NULL);
249 bool mstring_containsString (const char *s, const char *c)
251 if (mstring_isDefined (s))
253 return (strstr (s, c) != NULL);
261 char *mstring_concat (const char *s1, const char *s2)
263 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
269 extern /*@only@*/ char *
270 mstring_concatFree (/*@only@*/ char *s1, /*@only@*/ char *s2)
272 /* like mstring_concat but deallocates old strings */
273 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
282 extern /*@only@*/ char *
283 mstring_concatFree1 (/*@only@*/ char *s1, const char *s2)
285 char *s = (char *) dmalloc (strlen (s1) + strlen (s2) + 1);
293 extern /*@only@*/ char *
294 mstring_append (/*@only@*/ char *s1, char c)
296 size_t l = strlen (s1);
299 s = (char *) dmalloc (sizeof (*s) * (l + 2));
309 char *mstring_copy (char *s1)
317 char *s = (char *) dmalloc ((strlen (s1) + 1) * sizeof (*s));
324 char *mstring_safePrint (char *s)
328 return ("<undefined>");
337 char *mstring_create (int n)
341 s = dmalloc (sizeof (*s) * (n + 1));
347 fputline (FILE *out, char *s)
351 check (fputs (s, out) != EOF);
354 check (fputc ('\n', out) == (int) '\n');
372 longUnsigned_fromInt (int x)
376 return (long unsigned) x;
379 size_t size_fromInt (int x)
381 size_t res = (size_t) x;
383 llassert ((int) res == x);
387 int size_toInt (size_t x)
391 llassert ((size_t) res == x);
395 long size_toLong (size_t x)
399 llassert ((size_t) res == x);
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.
415 llassert ((int) res == x);
421 longUnsigned_toInt (long unsigned int x)
425 llassert ((long unsigned) res == x);
430 long_toInt (long int x)
434 /*@+ignorequals@*/ llassert (res == x); /*@=ignorequals@*/
440 bool mstring_equalPrefix (const char *c1, const char *c2)
442 llassert (c1 != NULL);
443 llassert (c2 != NULL);
445 if (strncmp(c1, c2, strlen(c2)) == 0)
455 bool mstring_equal (/*@null@*/ const char *s1, /*@null@*/ const char *s2)
469 return (strcmp(s1, s2) == 0);