Initial import
[samba] / source / lib / clobber.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Martin Pool     2003
5    Copyright (C) Andrew Bartlett 2003
6    
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 2 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #ifdef DEVELOPER
25 const char *global_clobber_region_function;
26 unsigned int global_clobber_region_line;
27 #endif
28
29 /**
30  * In developer builds, clobber a region of memory.
31  *
32  * If we think a string buffer is longer than it really is, this ought
33  * to make the failure obvious, by segfaulting (if in the heap) or by
34  * killing the return address (on the stack), or by trapping under a
35  * memory debugger.
36  *
37  * This is meant to catch possible string overflows, even if the
38  * actual string copied is not big enough to cause an overflow.
39  *
40  * In addition, under Valgrind the buffer is marked as uninitialized.
41  **/
42 void clobber_region(const char *fn, unsigned int line, char *dest, size_t len)
43 {
44 #ifdef DEVELOPER
45         global_clobber_region_function = fn;
46         global_clobber_region_line = line;
47
48         /* F1 is odd and 0xf1f1f1f1 shouldn't be a valid pointer */
49         memset(dest, 0xF1, len);
50 #ifdef VALGRIND
51         /* Even though we just wrote to this, from the application's
52          * point of view it is not initialized.
53          *
54          * (This is not redundant with the clobbering above.  The
55          * marking might not actually take effect if we're not running
56          * under valgrind.) */
57         VALGRIND_MAKE_WRITABLE(dest, len);
58 #endif /* VALGRIND */
59 #endif /* DEVELOPER */
60 }