summaryrefslogtreecommitdiffstats
path: root/source/history.c
diff options
context:
space:
mode:
authorDave Davenport <qball@gmpclient.org>2016-04-27 12:55:48 +0200
committerDave Davenport <qball@gmpclient.org>2016-04-27 12:57:43 +0200
commit4dbf2813d0b5bc31973a78bed75abfcaf8203353 (patch)
treedfc526418f7b49cc1429d1d21423d975eb20a993 /source/history.c
parent88c0a5c888747d207f2d5dc654af97956687b205 (diff)
Issue #388, Try to make history parser more robust against corruption.
Diffstat (limited to 'source/history.c')
-rw-r--r--source/history.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/source/history.c b/source/history.c
index 094b9bcc..07baef32 100644
--- a/source/history.c
+++ b/source/history.c
@@ -90,18 +90,28 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) {
char * start = NULL;
// Skip empty lines.
- if ( strlen ( buffer ) == 0 ) {
+ if ( l <= 1 ) {
+ continue;
+ }
+
+ long int index = strtol ( buffer, &start, 10 );
+ if ( start == buffer || *start == '\0' ) {
+ continue;
+ }
+ start++;
+ if ( (l - (start-buffer)) < 3) {
continue;
}
// Resize and check.
retv = g_realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
retv[( *length )] = g_malloc ( sizeof ( _element ) );
+
// remove trailing \n
- buffer[strlen ( buffer ) - 1] = '\0';
+ buffer[l - 1] = '\0';
// Parse the number of times.
- retv[( *length )]->index = strtol ( buffer, &start, 10 );
- retv[( *length )]->name = g_strndup ( start + 1, l - 1 - ( start + 1 - buffer ) );
+ retv[( *length )]->index = index;
+ retv[( *length )]->name = g_strndup ( start, l - 1 - ( start - buffer ) );
// Force trailing '\0'
retv[( *length ) + 1] = NULL;