s3: Fix bug #9085.
[Samba.git] / source3 / lib / clobber.c
blobe77e786fb5d675301316b08d0dbcbba680ca0d45
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Martin Pool 2003
5 Copyright (C) Andrew Bartlett 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 #ifdef DEVELOPER
24 const char *global_clobber_region_function;
25 unsigned int global_clobber_region_line;
26 #endif
28 /**
29 * In developer builds, clobber a region of memory.
31 * If we think a string buffer is longer than it really is, this ought
32 * to make the failure obvious, by segfaulting (if in the heap) or by
33 * killing the return address (on the stack), or by trapping under a
34 * memory debugger.
36 * This is meant to catch possible string overflows, even if the
37 * actual string copied is not big enough to cause an overflow.
39 * In addition, under Valgrind the buffer is marked as uninitialized.
40 **/
41 void clobber_region(const char *fn, unsigned int line, char *dest, size_t len)
43 #ifdef DEVELOPER
44 global_clobber_region_function = fn;
45 global_clobber_region_line = line;
47 /* F1 is odd and 0xf1f1f1f1 shouldn't be a valid pointer */
48 memset(dest, 0xF1, len);
49 #ifdef VALGRIND
50 /* Even though we just wrote to this, from the application's
51 * point of view it is not initialized.
53 * (This is not redundant with the clobbering above. The
54 * marking might not actually take effect if we're not running
55 * under valgrind.) */
56 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
57 VALGRIND_MAKE_MEM_UNDEFINED(dest, len);
58 #elif defined(VALGRIND_MAKE_WRITABLE)
59 VALGRIND_MAKE_WRITABLE(dest, len);
60 #endif
61 #endif /* VALGRIND */
62 #endif /* DEVELOPER */