author | Mahlon E. Smith <mahlon@martini.nu> |
Wed, 28 Sep 2011 09:04:16 -0700 | |
changeset 9 | bdf20e6eefd7 |
parent 5 | 7718f04c8cd1 |
child 10 | d07309450285 |
permissions | -rw-r--r-- |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
1 |
/* vim: set noet nosta sw=4 ts=4 ft=c : */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
2 |
/* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
3 |
Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu> |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
4 |
All rights reserved. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
5 |
Redistribution and use in source and binary forms, with or without |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
6 |
modification, are permitted provided that the following conditions are met: |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
7 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
8 |
* Redistributions of source code must retain the above copyright |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
9 |
notice, this list of conditions and the following disclaimer. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
10 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
11 |
* Redistributions in binary form must reproduce the above copyright |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
12 |
notice, this list of conditions and the following disclaimer in the |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
13 |
documentation and/or other materials provided with the distribution. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
14 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
15 |
* Neither the name of Mahlon E. Smith nor the names of his |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
16 |
contributors may be used to endorse or promote products derived |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
17 |
from this software without specific prior written permission. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
18 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
19 |
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
20 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
22 |
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
23 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
24 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
25 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
26 |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
27 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
28 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
29 |
*/ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
30 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
31 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
32 |
#include "volta.h" |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
33 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
34 |
/* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
35 |
* Output basic usage information. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
36 |
*/ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
37 |
void |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
38 |
usage( char *prg ) |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
39 |
{ |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
40 |
printf( "%s [-vh] [-f <filename>] [-d <level>]\n", prg ); |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
41 |
printf( " -v Display version\n" ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
42 |
printf( " -d <level> Show debug information on stderr\n" ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
43 |
printf( " -h Usage (you're lookin' at it)\n" ); |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
44 |
printf( " -f <filename> Specify the database to use (default is 'volta.db' in the cwd.)\n"); |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
45 |
printf( "\n" ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
46 |
return; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
47 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
48 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
49 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
50 |
/* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
51 |
* Debug function, only output to stderr if the debug level is |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
52 |
* equal or greater to the set output level. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
53 |
* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
54 |
* level: The minimum debug level that must be set for the |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
55 |
* line to be logged. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
56 |
* file: The current code file that is emitting the log |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
57 |
* line: The line number of the code file that is emitting the log |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
58 |
* ... : any printf style strings and formats that constitute the log message |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
59 |
*/ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
60 |
void |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
61 |
debug( int level, char *file, int line, const char *fmt, ... ) |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
62 |
{ |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
63 |
if ( v.debugmode < level ) return; |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
64 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
65 |
char timestamp[20]; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
66 |
time_t t = time( NULL ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
67 |
struct tm *now = localtime( &t ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
68 |
strftime( timestamp, 20, "%F %T", now ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
69 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
70 |
va_list args; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
71 |
va_start( args, fmt ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
72 |
fprintf( stderr, "%s [%s] #%d (%s:%04d): ", |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
73 |
PROG, timestamp, getpid(), file, line ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
74 |
vfprintf( stderr, fmt, args ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
75 |
va_end( args ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
76 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
77 |
return; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
78 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
79 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
80 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
81 |
/* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
82 |
* Append 'buf' to the end of 'line', a la strcat, except dynamically |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
83 |
* grow memory for the target string. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
84 |
* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
85 |
* 'buf' should be null terminated. Returns the modified line. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
86 |
*/ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
87 |
char * |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
88 |
extend_line( char *line, const char *buf ) |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
89 |
{ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
90 |
char *line_realloc; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
91 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
92 |
unsigned short int offset; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
93 |
size_t new_len; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
94 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
95 |
/* find offset and lengths, first assignment */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
96 |
if ( line == NULL ) { |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
97 |
offset = 0; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
98 |
new_len = strlen( buf ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
99 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
100 |
/* find offset and lengths, append to existing string */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
101 |
else { |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
102 |
offset = strlen( line ); /* not including '\0' */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
103 |
new_len = offset + LINE_BUFSIZE; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
104 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
105 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
106 |
debug( 3, LOC, "Extending line to %d bytes at offset %d...\n", new_len, offset ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
107 |
if ( (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) { |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
108 |
/* cleanup on allocation errors */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
109 |
debug( 3, LOC, "Ignoring line, error while allocating memory: %s\n", strerror(errno) ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
110 |
if ( line != NULL ) free( line ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
111 |
line = NULL; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
112 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
113 |
else { |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
114 |
line = line_realloc; |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
115 |
memcpy( line + offset, buf, LINE_BUFSIZE - 1 ); |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
116 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
117 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
118 |
return( line ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
119 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
120 |
|
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
121 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
122 |
/* |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
123 |
* Read an entire file into memory, returning a pointer the contents. |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
124 |
* Returns NULL on error. |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
125 |
* |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
126 |
*/ |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
127 |
char * |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
128 |
slurp_file( char *file ) |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
129 |
{ |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
130 |
FILE *fh = NULL; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
131 |
char *contents = NULL; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
132 |
struct stat sb; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
133 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
134 |
if ( stat( file, &sb ) != 0 ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
135 |
debug( 1, LOC, "Unable to stat() file '%s': %s\n", |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
136 |
file, strerror(errno) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
137 |
return( NULL ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
138 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
139 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
140 |
if ( (contents = malloc( sb.st_size + 1 )) == NULL ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
141 |
debug( 1, LOC, "Unable to allocate memory for file '%s': %s\n", |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
142 |
file, strerror(errno) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
143 |
return( NULL ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
144 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
145 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
146 |
if ( (fh = fopen( file, "r" )) == NULL ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
147 |
debug( 1, LOC, "Could not open file for reading '%s': %s\n", |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
148 |
file, strerror(errno) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
149 |
return( NULL ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
150 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
151 |
|
5
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
152 |
if ( fread( contents, sizeof(char), sb.st_size, fh ) != sb.st_size ) { |
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
153 |
debug( 1, LOC, "Short read for file '%s'?: %s\n", file ); |
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
154 |
fclose( fh ); |
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
155 |
return( NULL ); |
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
156 |
} |
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
157 |
|
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
158 |
fclose( fh ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
159 |
return( contents ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
160 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
161 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
162 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
163 |