From 68f239bc9011dcf756815e18fcc4be7a77b4dd33 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sat, 18 Apr 2009 12:14:27 +0000 Subject: [PATCH] * persistantly handle the bufferlengths for dates; snprintf doesn't terminate strings if they exceed the provided length, which was causing character soup in some rare cases. --- webcit/calendar.c | 4 ++-- webcit/calendar_view.c | 24 ++++++++++++------------ webcit/fmt_date.c | 30 +++++++++++++++--------------- webcit/msg_renderers.c | 6 +++--- webcit/smtpqueue.c | 4 ++-- webcit/webcit.h | 6 ++---- 6 files changed, 36 insertions(+), 38 deletions(-) diff --git a/webcit/calendar.c b/webcit/calendar.c index 8b9563804..8808618c0 100644 --- a/webcit/calendar.c +++ b/webcit/calendar.c @@ -125,7 +125,7 @@ void cal_process_object(StrBuf *Target, } else { tt = icaltime_as_timet(t); - webcit_fmt_date(buf, tt, DATEFMT_FULL); + webcit_fmt_date(buf, 256, tt, DATEFMT_FULL); StrBufAppendPrintf(Target, "
"); StrBufAppendPrintf(Target, _("Starting date/time:")); StrBufAppendPrintf(Target, "
%s
", buf); @@ -136,7 +136,7 @@ void cal_process_object(StrBuf *Target, if (p != NULL) { t = icalproperty_get_dtend(p); tt = icaltime_as_timet(t); - webcit_fmt_date(buf, tt, DATEFMT_FULL); + webcit_fmt_date(buf, 256, tt, DATEFMT_FULL); StrBufAppendPrintf(Target, "
"); StrBufAppendPrintf(Target, _("Ending date/time:")); StrBufAppendPrintf(Target, "
%s
", buf); diff --git a/webcit/calendar_view.c b/webcit/calendar_view.c index 41a03ac39..6d71049a9 100644 --- a/webcit/calendar_view.c +++ b/webcit/calendar_view.c @@ -335,7 +335,7 @@ void calendar_month_view_display_events(int year, int month, int day) } else { tt = icaltime_as_timet(t); - webcit_fmt_date(buf, tt, DATEFMT_BRIEF); + webcit_fmt_date(buf, 256, tt, DATEFMT_BRIEF); if (no_end || !icaltime_compare(t, end_t)) { wprintf("%s %s
", _("Date/time:"), buf); @@ -344,7 +344,7 @@ void calendar_month_view_display_events(int year, int month, int day) wprintf("%s %s
", _("Starting date/time:"), buf); tt = icaltime_as_timet(end_t); - webcit_fmt_date(buf, tt, DATEFMT_BRIEF); + webcit_fmt_date(buf, 256, tt, DATEFMT_BRIEF); wprintf("%s %s
", _("Ending date/time:"), buf); } @@ -946,13 +946,13 @@ void calendar_day_view_display_events(time_t thetime, wprintf("
"); } if (!icaltime_compare(t, end_t)) { /* one day only */ - webcit_fmt_date(buf, event_tt, DATEFMT_LOCALEDATE); + webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE); wprintf("%s %s
", _("Date:"), buf); } else { - webcit_fmt_date(buf, event_tt, DATEFMT_LOCALEDATE); + webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE); wprintf("%s %s
", _("Starting date:"), buf); - webcit_fmt_date(buf, event_tte, DATEFMT_LOCALEDATE); + webcit_fmt_date(buf, 256, event_tte, DATEFMT_LOCALEDATE); wprintf("%s %s
", _("Ending date:"), buf); } q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY); @@ -987,9 +987,9 @@ void calendar_day_view_display_events(time_t thetime, escputs((char *)icalproperty_get_comment(q)); wprintf("
"); } - webcit_fmt_date(buf, event_tt, DATEFMT_BRIEF); + webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF); wprintf("%s %s
", _("Starting date/time:"), buf); - webcit_fmt_date(buf, event_tte, DATEFMT_BRIEF); + webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF); wprintf("%s %s
", _("Ending date/time:"), buf); q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY); if (q) { @@ -1067,13 +1067,13 @@ void calendar_day_view_display_events(time_t thetime, wprintf("
"); } if (!icaltime_compare(t, end_t)) { /* one day only */ - webcit_fmt_date(buf, event_tt, DATEFMT_BRIEF); + webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF); wprintf("%s %s
", _("Date/time:"), buf); } else { - webcit_fmt_date(buf, event_tt, DATEFMT_BRIEF); + webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF); wprintf("%s %s
", _("Starting date/time:"), buf); - webcit_fmt_date(buf, event_tte, DATEFMT_BRIEF); + webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF); wprintf("%s %s
", _("Ending date/time:"), buf); } q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY); @@ -1359,7 +1359,7 @@ int calendar_summary_view(void) { else { all_day_event = 0; } - fmt_time(timestring, event_tt); + fmt_time(timestring, SIZ, event_tt); if (all_day_event) { gmtime_r(&event_tt, &event_tm); @@ -1659,7 +1659,7 @@ void do_tasks_view(void) { due = get_task_due_date(Cal->cal, &is_date); wprintf(" 0) { - webcit_fmt_date(buf, due, is_date ? DATEFMT_RAWDATE : DATEFMT_FULL); + webcit_fmt_date(buf, SIZ, due, is_date ? DATEFMT_RAWDATE : DATEFMT_FULL); wprintf(">%s",buf); } else { diff --git a/webcit/fmt_date.c b/webcit/fmt_date.c index ecc7b78bb..40419ac2e 100644 --- a/webcit/fmt_date.c +++ b/webcit/fmt_date.c @@ -47,7 +47,7 @@ size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm) /* * Format a date/time stamp for output */ -void webcit_fmt_date(char *buf, time_t thetime, int Format) +void webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format) { struct tm tm; struct tm today_tm; @@ -76,31 +76,31 @@ void webcit_fmt_date(char *buf, time_t thetime, int Format) &&(tm.tm_mon == today_tm.tm_mon) &&(tm.tm_mday == today_tm.tm_mday)) { if (time_format == WC_TIMEFORMAT_24) - wc_strftime(buf, 32, "%k:%M", &tm); + wc_strftime(buf, siz, "%k:%M", &tm); else - wc_strftime(buf, 32, "%l:%M%p", &tm); + wc_strftime(buf, siz, "%l:%M%p", &tm); } else if (today_timet - thetime < 15552000) { if (time_format == WC_TIMEFORMAT_24) - wc_strftime(buf, 32, "%b %d %k:%M", &tm); + wc_strftime(buf, siz, "%b %d %k:%M", &tm); else - wc_strftime(buf, 32, "%b %d %l:%M%p", &tm); + wc_strftime(buf, siz, "%b %d %l:%M%p", &tm); } else { - wc_strftime(buf, 32, "%b %d %Y", &tm); + wc_strftime(buf, siz, "%b %d %Y", &tm); } break; case DATEFMT_FULL: if (time_format == WC_TIMEFORMAT_24) - wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm); + wc_strftime(buf, siz, "%a %b %d %Y %T %Z", &tm); else - wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm); + wc_strftime(buf, siz, "%a %b %d %Y %r %Z", &tm); break; case DATEFMT_RAWDATE: - wc_strftime(buf, 32, "%a %b %d %Y", &tm); + wc_strftime(buf, siz, "%a %b %d %Y", &tm); break; case DATEFMT_LOCALEDATE: - wc_strftime(buf, 32, "%x", &tm); + wc_strftime(buf, siz, "%x", &tm); break; } } @@ -110,10 +110,10 @@ void webcit_fmt_date(char *buf, time_t thetime, int Format) * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale. */ long guess_calhourformat(void) { - char buf[32]; + char buf[64]; struct tm tm; memset(&tm, 0, sizeof tm); - wc_strftime(buf, 32, "%X", &tm); + wc_strftime(buf, 64, "%X", &tm); if (buf[strlen(buf)-1] == 'M') { return 12; } @@ -154,7 +154,7 @@ int get_time_format_cached (void) * buf the output buffer * thetime time to format into buf */ -void fmt_time(char *buf, time_t thetime) +void fmt_time(char *buf, size_t siz, time_t thetime) { struct tm *tm; int hour; @@ -170,12 +170,12 @@ void fmt_time(char *buf, time_t thetime) hour = hour - 12; if (time_format == WC_TIMEFORMAT_24) { - sprintf(buf, "%d:%02d", + snprintf(buf, siz, "%d:%02d", tm->tm_hour, tm->tm_min ); } else { - sprintf(buf, "%d:%02d%s", + snprintf(buf, siz, "%d:%02d%s", hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am") ); } diff --git a/webcit/msg_renderers.c b/webcit/msg_renderers.c index b401ea759..5481cdceb 100644 --- a/webcit/msg_renderers.c +++ b/webcit/msg_renderers.c @@ -441,7 +441,7 @@ void tmplput_MAIL_SUMM_DATE_BRIEF(StrBuf *Target, WCTemplputParams *TP) { char datebuf[64]; message_summary *Msg = (message_summary*) CTX; - webcit_fmt_date(datebuf, Msg->date, DATEFMT_BRIEF); + webcit_fmt_date(datebuf, 64, Msg->date, DATEFMT_BRIEF); StrBufAppendBufPlain(Target, datebuf, -1, 0); } @@ -449,13 +449,13 @@ void tmplput_MAIL_SUMM_DATE_FULL(StrBuf *Target, WCTemplputParams *TP) { char datebuf[64]; message_summary *Msg = (message_summary*) CTX; - webcit_fmt_date(datebuf, Msg->date, DATEFMT_FULL); + webcit_fmt_date(datebuf, 64, Msg->date, DATEFMT_FULL); StrBufAppendBufPlain(Target, datebuf, -1, 0); } void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, WCTemplputParams *TP) { message_summary *Msg = (message_summary*) CTX; - StrBufAppendPrintf(Target, "%ld", Msg->date, 0); + StrBufAppendPrintf(Target, 64, "%ld", Msg->date, 0); } diff --git a/webcit/smtpqueue.c b/webcit/smtpqueue.c index 43f5cd90a..db7e4c19f 100644 --- a/webcit/smtpqueue.c +++ b/webcit/smtpqueue.c @@ -137,7 +137,7 @@ void display_queue_msg(long msgnum) wprintf(""); if (submitted > 0) { - webcit_fmt_date(buf, submitted, 1); + webcit_fmt_date(buf, 1024, submitted, 1); wprintf("%s", buf); } else { @@ -146,7 +146,7 @@ void display_queue_msg(long msgnum) wprintf(""); if (last_attempt > 0) { - webcit_fmt_date(buf, last_attempt, 1); + webcit_fmt_date(buf, 1024, last_attempt, 1); wprintf("%s", buf); } else { diff --git a/webcit/webcit.h b/webcit/webcit.h index 832ed0dc6..90404821d 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -623,7 +623,7 @@ void smart_goto(const StrBuf *); void worker_entry(void); void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *ReqType, StrBuf *ReadBuf); size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm); -void fmt_time(char *buf, time_t thetime); +void fmt_time(char *buf, size_t siz, time_t thetime); void httpdate(char *buf, time_t thetime); time_t httpdate_to_timestamp(StrBuf *buf); void end_webcit_session(void); @@ -643,8 +643,6 @@ char *load_mimepart(long msgnum, char *partnum); void MimeLoadData(wc_mime_attachment *Mime); int pattern2(char *search, char *patn); void do_edit_vcard(long, char *, char *, const char *); -void striplt(char *); -void stripltlen(char *, int *); void select_user_to_edit(char *message, char *preselect); void delete_user(char *); void do_change_view(int); @@ -733,7 +731,7 @@ const char *get_selected_language(void); #define DATEFMT_BRIEF 1 #define DATEFMT_RAWDATE 2 #define DATEFMT_LOCALEDATE 3 -void webcit_fmt_date(char *buf, time_t thetime, int Format); +void webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format); int fetch_http(char *url, char *target_buf, int maxbytes); void free_attachments(wcsession *sess); void summary(void); -- 2.30.2