BRL-CAD
bu_badmagic.c
Go to the documentation of this file.
1 /* B U _ B A D M A G I C . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "bu.h"
27 
28 static int
29 bomb_callback(const void *data, const char *str)
30 {
31  char *expected_str;
32  int result;
33  if (!data) exit(1);
34  expected_str = (char *)data;
35  result = bu_strcmp(expected_str, str);
36  exit(result);
37 }
38 
39 int
40 main(int argc, char *argv[])
41 {
42  unsigned char *misalign = (unsigned char *)bu_malloc(1, "bu_badmagic.c");
43  uint32_t *ptr = (uint32_t *)bu_malloc(sizeof(uint32_t), "bu_badmagic.c");
44  uint32_t magic;
45  char *str = (char *)bu_malloc(20, "bu_badmagic.c");
46  char *expected_str = (char *)bu_malloc(512, "bu_badmagic.c");
47  char *file = "bu_badmagic.c";
48  int line = 42, testnum;
49 
50  if (argc < 2) {
51  bu_exit(1, "Must specify a function number. [%s]\n", argv[0]);
52  }
53 
54  bu_bomb_add_hook((bu_hook_t)&bomb_callback, (void *)expected_str);
55 
56  sscanf(argv[1], "%d", &testnum);
57  switch (testnum) {
58  case 1:
59  *ptr = BU_AVS_MAGIC;
60  magic = BU_BITV_MAGIC;
61  str = (char *)bu_identify_magic(*(ptr));
62  sprintf(expected_str, "ERROR: bad pointer %p: s/b %s(x%lx), was %s(x%lx), file %s, line %d\n",
63  (void *)ptr,
64  str, (unsigned long)magic,
65  bu_identify_magic(*(ptr)), (unsigned long)*(ptr),
66  file, line);
67  bu_badmagic(ptr, magic, str, file, line);
68  return 1;
69  case 2:
70  *ptr = BU_BITV_MAGIC;
71  magic = BU_BITV_MAGIC;
72  str = (char *)bu_identify_magic(*ptr);
73  expected_str = "\0";
74  bu_badmagic(ptr, magic, str, file, line);
75  return 0;
76  case 3:
77  ptr = NULL;
78  magic = BU_COLOR_MAGIC;
79  str = (char *)bu_identify_magic(magic);
80  sprintf(expected_str, "ERROR: NULL %s pointer, file %s, line %d\n",
81  str, file, line);
82  bu_badmagic(ptr, magic, str, file, line);
83  return 1;
84  case 4:
85  misalign = (unsigned char *)ptr;
86  ptr = (uint32_t *)(misalign + 1);
87  magic = BU_EXTERNAL_MAGIC;
88  str = (char *)bu_identify_magic(magic);
89  sprintf(expected_str, "ERROR: %p mis-aligned %s pointer, file %s, line %d\n",
90  (void *)ptr, str, file, line);
91  bu_badmagic(ptr, magic, str, file, line);
92  return 1;
93  }
94 
95  bu_log("Invalid function number %d specified. [%s]\n", testnum, argv[0]);
96  return 1;
97 }
98 
99 /*
100  * Local Variables:
101  * mode: C
102  * tab-width: 8
103  * indent-tabs-mode: t
104  * c-file-style: "stroustrup"
105  * End:
106  * ex: shiftwidth=4 tabstop=8
107  */
#define BU_COLOR_MAGIC
Definition: magic.h:48
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#define BU_AVS_MAGIC
Definition: magic.h:46
Header file for the BRL-CAD common definitions.
int main(int argc, char *argv[])
Definition: bu_badmagic.c:40
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
COMPLEX data[64]
Definition: fftest.c:34
#define BU_BITV_MAGIC
Definition: magic.h:47
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
oldeumate l2 magic
Definition: nmg_mod.c:3843
void bu_badmagic(const uint32_t *ptr, uint32_t magic, const char *str, const char *file, int line)
Definition: badmagic.c:31
int bu_strcmp(const char *string1, const char *string2)
Definition: str.c:171
int(* bu_hook_t)(void *, void *)
Definition: log.h:68
void bu_bomb_add_hook(bu_hook_t func, void *clientdata)
Definition: bomb.c:84
#define BU_EXTERNAL_MAGIC
Definition: magic.h:49
const char * bu_identify_magic(uint32_t magic)