From 0529b864b0b2ababd799ee18faf486dbcce68b97 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Tue, 24 Jun 2014 23:45:02 +0200
Subject: Fix segmentation fault

This adds some more accurate checks to avoid a segmentation fault that
occurred when accessing a nonexistent item.

Fixes GitHub issue #7.

Reported-by: Bromind <martin.vassor@hotmail.fr>
Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/args.c     |  2 +-
 src/calcurse.h |  2 +-
 src/day.c      |  6 ++++--
 src/ui-day.c   | 20 ++++++++++----------
 4 files changed, 16 insertions(+), 14 deletions(-)

(limited to 'src')

diff --git a/src/args.c b/src/args.c
index f18629c..8b62cb8 100644
--- a/src/args.c
+++ b/src/args.c
@@ -309,7 +309,7 @@ app_arg(int add_line, struct date *day, long date, const char *fmt_apt,
 
 	day_store_items(date, regex, 0);
 
-	int n = day_item_count();
+	int n = day_item_count(0);
 
 	if (n > 0) {
 		if (add_line)
diff --git a/src/calcurse.h b/src/calcurse.h
index 4b804c3..3c4bcb0 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -713,7 +713,7 @@ unsigned day_chk_busy_slices(struct date, int, int *);
 struct day_item *day_cut_item(long, int);
 int day_paste_item(struct day_item *, long);
 struct day_item *day_get_item(int);
-unsigned day_item_count(void);
+unsigned day_item_count(int);
 void day_edit_note(struct day_item *, const char *);
 void day_view_note(struct day_item *, const char *);
 void day_item_switch_notify(struct day_item *);
diff --git a/src/day.c b/src/day.c
index 15daa41..5b5a3e3 100644
--- a/src/day.c
+++ b/src/day.c
@@ -43,6 +43,7 @@
 #include "calcurse.h"
 
 static vector_t day_items;
+static unsigned day_items_nb = 0;
 
 static void day_free(struct day_item *day)
 {
@@ -359,6 +360,7 @@ day_store_items(long date, regex_t * regex, int include_captions)
 		day_add_item(DAY_SEPARATOR, 0, p);
 
 	VECTOR_SORT(&day_items, day_cmp_start);
+	day_items_nb = events + apts;
 }
 
 /*
@@ -682,9 +684,9 @@ struct day_item *day_get_item(int item_number)
 	return VECTOR_NTH(&day_items, item_number);
 }
 
-unsigned day_item_count(void)
+unsigned day_item_count(int include_captions)
 {
-	return VECTOR_COUNT(&day_items);
+	return (include_captions ? VECTOR_COUNT(&day_items) : day_items_nb);
 }
 
 /* Attach a note to an appointment or event. */
diff --git a/src/ui-day.c b/src/ui-day.c
index 8c34e77..d6be872 100644
--- a/src/ui-day.c
+++ b/src/ui-day.c
@@ -298,7 +298,7 @@ void ui_day_item_edit(void)
 	struct apoint *a;
 	int need_check_notify = 0;
 
-	if (day_item_count() <= 0)
+	if (day_item_count(0) <= 0)
 		return;
 
 	struct day_item *p = day_get_item(listbox_get_sel(&lb_apt));
@@ -412,7 +412,7 @@ void ui_day_item_pipe(void)
 	int pid;
 	FILE *fpout;
 
-	if (day_item_count() <= 0)
+	if (day_item_count(0) <= 0)
 		return;
 
 	struct day_item *p = day_get_item(listbox_get_sel(&lb_apt));
@@ -575,7 +575,7 @@ void ui_day_item_delete(unsigned reg)
 	const int nb_note_choices = 2;
 	long date = ui_calendar_get_slctd_day_sec();
 
-	if (day_item_count() <= 0)
+	if (day_item_count(0) <= 0)
 		return;
 
 	struct day_item *p = day_get_item(listbox_get_sel(&lb_apt));
@@ -668,7 +668,7 @@ void ui_day_item_repeat(void)
 	struct recur_apoint *ra;
 	long until, date;
 
-	if (day_item_count() <= 0)
+	if (day_item_count(0) <= 0)
 		return;
 
 	item_nb = listbox_get_sel(&lb_apt);
@@ -797,7 +797,7 @@ void ui_day_item_cut_free(unsigned reg)
 /* Copy an item, so that it can be pasted somewhere else later. */
 void ui_day_item_copy(unsigned reg)
 {
-	if (day_item_count() <= 0 || reg == REG_BLACK_HOLE)
+	if (day_item_count(0) <= 0 || reg == REG_BLACK_HOLE)
 		return;
 
 	struct day_item *item = day_get_item(listbox_get_sel(&lb_apt));
@@ -821,7 +821,7 @@ void ui_day_item_paste(unsigned reg)
 
 void ui_day_load_items(void)
 {
-	listbox_load_items(&lb_apt, day_item_count());
+	listbox_load_items(&lb_apt, day_item_count(1));
 }
 
 void ui_day_sel_reset(void)
@@ -889,7 +889,7 @@ void ui_day_update_panel(int which_pan)
 
 void ui_day_popup_item(void)
 {
-	if (day_item_count() <= 0)
+	if (day_item_count(0) <= 0)
 		return;
 
 	struct day_item *item = day_get_item(listbox_get_sel(&lb_apt));
@@ -898,7 +898,7 @@ void ui_day_popup_item(void)
 
 void ui_day_flag(void)
 {
-	if (day_item_count() <= 0)
+	if (day_item_count(0) <= 0)
 		return;
 
 	struct day_item *item = day_get_item(listbox_get_sel(&lb_apt));
@@ -907,7 +907,7 @@ void ui_day_flag(void)
 
 void ui_day_view_note(void)
 {
-	if (day_item_count() <= 0)
+	if (day_item_count(0) <= 0)
 		return;
 
 	struct day_item *item = day_get_item(listbox_get_sel(&lb_apt));
@@ -916,7 +916,7 @@ void ui_day_view_note(void)
 
 void ui_day_edit_note(void)
 {
-	if (day_item_count() <= 0)
+	if (day_item_count(0) <= 0)
 		return;
 
 	struct day_item *item = day_get_item(listbox_get_sel(&lb_apt));
-- 
cgit v1.2.3-70-g09d2