2 /***************************************************************************
3 * nbase_memalloc.c -- A few simple functions related to memory *
4 * allocation. Right now they are just safe_ versions of well known calls *
5 * like malloc that quit if the allocation fails (so you don't have to *
6 * have a bunch of ugly checks in your code. These were written by *
7 * fyodor@insecure.org . *
9 ***********************IMPORTANT NMAP LICENSE TERMS************************
11 * The Nmap Security Scanner is (C) 1996-2006 Insecure.Com LLC. Nmap is *
12 * also a registered trademark of Insecure.Com LLC. This program is free *
13 * software; you may redistribute and/or modify it under the terms of the *
14 * GNU General Public License as published by the Free Software *
15 * Foundation; Version 2 with the clarifications and exceptions described *
16 * below. This guarantees your right to use, modify, and redistribute *
17 * this software under certain conditions. If you wish to embed Nmap *
18 * technology into proprietary software, we sell alternative licenses *
19 * (contact sales@insecure.com). Dozens of software vendors already *
20 * license Nmap technology such as host discovery, port scanning, OS *
21 * detection, and version detection. *
23 * Note that the GPL places important restrictions on "derived works", yet *
24 * it does not provide a detailed definition of that term. To avoid *
25 * misunderstandings, we consider an application to constitute a *
26 * "derivative work" for the purpose of this license if it does any of the *
28 * o Integrates source code from Nmap *
29 * o Reads or includes Nmap copyrighted data files, such as *
30 * nmap-os-fingerprints or nmap-service-probes. *
31 * o Executes Nmap and parses the results (as opposed to typical shell or *
32 * execution-menu apps, which simply display raw Nmap output and so are *
33 * not derivative works.) *
34 * o Integrates/includes/aggregates Nmap into a proprietary executable *
35 * installer, such as those produced by InstallShield. *
36 * o Links to a library or executes a program that does any of the above *
38 * The term "Nmap" should be taken to also include any portions or derived *
39 * works of Nmap. This list is not exclusive, but is just meant to *
40 * clarify our interpretation of derived works with some common examples. *
41 * These restrictions only apply when you actually redistribute Nmap. For *
42 * example, nothing stops you from writing and selling a proprietary *
43 * front-end to Nmap. Just distribute it by itself, and point people to *
44 * http://insecure.org/nmap/ to download Nmap. *
46 * We don't consider these to be added restrictions on top of the GPL, but *
47 * just a clarification of how we interpret "derived works" as it applies *
48 * to our GPL-licensed Nmap product. This is similar to the way Linus *
49 * Torvalds has announced his interpretation of how "derived works" *
50 * applies to Linux kernel modules. Our interpretation refers only to *
51 * Nmap - we don't speak for any other GPL products. *
53 * If you have any questions about the GPL licensing restrictions on using *
54 * Nmap in non-GPL works, we would be happy to help. As mentioned above, *
55 * we also offer alternative license to integrate Nmap into proprietary *
56 * applications and appliances. These contracts have been sold to dozens *
57 * of software vendors, and generally include a perpetual license as well *
58 * as providing for priority support and updates as well as helping to *
59 * fund the continued development of Nmap technology. Please email *
60 * sales@insecure.com for further information. *
62 * As a special exception to the GPL terms, Insecure.Com LLC grants *
63 * permission to link the code of this program with any version of the *
64 * OpenSSL library which is distributed under a license identical to that *
65 * listed in the included Copying.OpenSSL file, and distribute linked *
66 * combinations including the two. You must obey the GNU GPL in all *
67 * respects for all of the code used other than OpenSSL. If you modify *
68 * this file, you may extend this exception to your version of the file, *
69 * but you are not obligated to do so. *
71 * If you received these files with a written license agreement or *
72 * contract stating terms other than the terms above, then that *
73 * alternative license agreement takes precedence over these comments. *
75 * Source is provided to this software because we believe users have a *
76 * right to know exactly what a program is going to do before they run it. *
77 * This also allows you to audit the software for security holes (none *
78 * have been found so far). *
80 * Source code also allows you to port Nmap to new platforms, fix bugs, *
81 * and add new features. You are highly encouraged to send your changes *
82 * to fyodor@insecure.org for possible incorporation into the main *
83 * distribution. By sending these changes to Fyodor or one the *
84 * Insecure.Org development mailing lists, it is assumed that you are *
85 * offering Fyodor and Insecure.Com LLC the unlimited, non-exclusive right *
86 * to reuse, modify, and relicense the code. Nmap will always be *
87 * available Open Source, but this is important because the inability to *
88 * relicense code has caused devastating problems for other Free Software *
89 * projects (such as KDE and NASM). We also occasionally relicense the *
90 * code to third parties as discussed above. If you wish to specify *
91 * special license conditions of your contributions, just say so when you *
94 * This program is distributed in the hope that it will be useful, but *
95 * WITHOUT ANY WARRANTY; without even the implied warranty of *
96 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
97 * General Public License for more details at *
98 * http://www.gnu.org/copyleft/gpl.html , or in the COPYING file included *
101 ***************************************************************************/
103 /* $Id: nbase_memalloc.c 3899 2006-08-29 05:42:35Z fyodor $ */
108 static void fatal(char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
110 static void fatal(char *fmt, ...)
115 vfprintf(stderr, fmt, ap);
116 fprintf(stderr, "\nQUITTING!\n");
121 void *safe_malloc(size_t size)
124 if ((int) size < 0) /* Catch caller errors */
125 fatal("Tried to malloc negative amount of memory!!!");
126 mymem = malloc(size);
128 fatal("Malloc Failed! Probably out of space.");
132 void *safe_realloc(void *ptr, size_t size)
135 if ((int) size < 0) /* Catch caller errors */
136 fatal("Tried to realloc negative amount of memory!!!");
137 mymem = realloc(ptr, size);
139 fatal("Realloc Failed! Probably out of space.");
143 /* Zero-initializing version of safe_malloc */
144 void *safe_zalloc(size_t size)
148 fatal("Tried to malloc negative amount of memory!!!");
149 mymem = calloc(1, size);
151 fatal("Malloc Failed! Probably out of space.");