1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-18 13:54:36 -05:00

Fix crash in history when removing last entry.

This commit is contained in:
QC 2014-08-29 12:14:43 +02:00
parent 5b017b017b
commit 169c05cc15
3 changed files with 116 additions and 1 deletions

View file

@ -52,7 +52,7 @@ static int __element_sort_func ( const void *ea, const void *eb )
static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length )
{
if ( list == NULL ) {
if ( list == NULL || length == 0 ) {
return;
}
// Sort the list before writing out.
@ -235,6 +235,8 @@ void history_remove ( const char *filename, const char *entry )
char ** history_get_list ( const char *filename, unsigned int *length )
{
*length = 0;
if ( config.disable_history ) {
return NULL;
}

13
test/Makefile Normal file
View file

@ -0,0 +1,13 @@
CC=gcc
CFLAGS=-I../include/ -I../ -std=c99 -fprofile-arcs -ftest-coverage -g3
SOURCES=\
history-test.c\
../config/config.c\
../source/history.c
history-test: $(SOURCES)
$(CC) -o $@ $^ `pkg-config --libs --cflags glib-2.0` $(CFLAGS)
test: history-test
./$^

100
test/history-test.c Normal file
View file

@ -0,0 +1,100 @@
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <glib.h>
#include <history.h>
#include <string.h>
static int test = 0;
#define TASSERT(a) {\
assert ( a );\
printf("Test %i passed (%s)\n", ++test, #a);\
}
const char *file = "text";
int main ( int argc, char **argv )
{
unlink(file);
// Empty list.
unsigned int length= 0;
char **retv = history_get_list ( file, &length);
TASSERT ( retv == NULL );
TASSERT ( length == 0 );
// 1 item
history_set( file, "aap");
retv = history_get_list ( file, &length);
TASSERT ( retv != NULL );
TASSERT ( length == 1 );
TASSERT ( strcmp(retv[0], "aap") == 0 );
g_strfreev(retv);
// Remove entry
history_remove ( file, "aap" );
length= 0;
retv = history_get_list ( file, &length);
TASSERT ( retv == NULL );
TASSERT ( length == 0 );
// 2 items
history_set( file, "aap");
history_set( file, "aap");
retv = history_get_list ( file, &length);
TASSERT ( retv != NULL );
TASSERT ( length == 1 );
TASSERT ( strcmp(retv[0], "aap") == 0 );
g_strfreev(retv);
for(int in=length+1; in < 26; in++) {
char *p = g_strdup_printf("aap%i", in);
printf("%s- %d\n",p, in);
history_set( file, p);
retv = history_get_list ( file, &length);
TASSERT ( retv != NULL );
TASSERT ( length == (in) );
g_strfreev(retv);
g_free(p);
}
// Max 25 entries.
history_set( file, "blaat" );
retv = history_get_list ( file, &length);
TASSERT ( retv != NULL );
TASSERT ( length == 25 );
g_strfreev(retv);
// Test fail.
history_set ( NULL, "aap");
retv = history_get_list ( NULL , &length);
printf("Test %i passed\n", ++test);
TASSERT ( retv == NULL );
TASSERT ( length == 0 );
history_remove ( NULL, "aap" );
printf("Test %i passed\n", ++test);
return 0;
}