Test struct tm* returns from gnc_gmtime and gnc_localtime

To ensure that we don't crash for dereffing a nullptr.
This commit is contained in:
John Ralls 2017-12-23 15:40:01 -08:00
parent a17bc85a02
commit a6a46d7cdc
4 changed files with 50 additions and 36 deletions

View File

@ -21,6 +21,7 @@
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
\********************************************************************/
%include "constraints.i"
typedef void * gpointer; // Not sure why SWIG doesn't figure this out.
%typemap(newfree) gchar * "g_free($1);"
@ -93,22 +94,25 @@ typedef char gchar;
%typemap(out) struct tm * {
SCM tm = scm_c_make_vector(11, SCM_UNDEFINED);
struct tm* t = $1;
SCM_SIMPLE_VECTOR_SET(tm, 0, scm_from_int(t->tm_sec));
SCM_SIMPLE_VECTOR_SET(tm, 1, scm_from_int(t->tm_min));
SCM_SIMPLE_VECTOR_SET(tm, 2, scm_from_int(t->tm_hour));
SCM_SIMPLE_VECTOR_SET(tm, 3, scm_from_int(t->tm_mday));
SCM_SIMPLE_VECTOR_SET(tm, 4, scm_from_int(t->tm_mon));
SCM_SIMPLE_VECTOR_SET(tm, 5, scm_from_int(t->tm_year));
SCM_SIMPLE_VECTOR_SET(tm, 6, scm_from_int(t->tm_wday));
SCM_SIMPLE_VECTOR_SET(tm, 7, scm_from_int(t->tm_yday));
SCM_SIMPLE_VECTOR_SET(tm, 8, scm_from_int(t->tm_isdst));
if (t != NULL)
{
SCM_SIMPLE_VECTOR_SET(tm, 0, scm_from_int(t->tm_sec));
SCM_SIMPLE_VECTOR_SET(tm, 1, scm_from_int(t->tm_min));
SCM_SIMPLE_VECTOR_SET(tm, 2, scm_from_int(t->tm_hour));
SCM_SIMPLE_VECTOR_SET(tm, 3, scm_from_int(t->tm_mday));
SCM_SIMPLE_VECTOR_SET(tm, 4, scm_from_int(t->tm_mon));
SCM_SIMPLE_VECTOR_SET(tm, 5, scm_from_int(t->tm_year));
SCM_SIMPLE_VECTOR_SET(tm, 6, scm_from_int(t->tm_wday));
SCM_SIMPLE_VECTOR_SET(tm, 7, scm_from_int(t->tm_yday));
SCM_SIMPLE_VECTOR_SET(tm, 8, scm_from_int(t->tm_isdst));
%#ifdef HAVE_STRUCT_TM_GMTOFF
SCM_SIMPLE_VECTOR_SET(tm, 9, scm_from_long(t->tm_gmtoff));
SCM_SIMPLE_VECTOR_SET(tm, 10, scm_from_locale_string(t->tm_zone?t->tm_zone:"Unset"));
SCM_SIMPLE_VECTOR_SET(tm, 9, scm_from_long(t->tm_gmtoff));
SCM_SIMPLE_VECTOR_SET(tm, 10, scm_from_locale_string(t->tm_zone?t->tm_zone:"Unset"));
%#else
SCM_SIMPLE_VECTOR_SET(tm, 9, scm_from_long(0));
SCM_SIMPLE_VECTOR_SET(tm, 10, scm_from_locale_string("GMT"));
SCM_SIMPLE_VECTOR_SET(tm, 9, scm_from_long(0));
SCM_SIMPLE_VECTOR_SET(tm, 10, scm_from_locale_string("GMT"));
%#endif
}
$result = tm;
}
@ -117,22 +121,25 @@ typedef char gchar;
%typemap(argout) struct tm * {
struct tm* t = $1;
SCM tm = $input;
SCM_SIMPLE_VECTOR_SET(tm, 0, scm_from_int(t->tm_sec));
SCM_SIMPLE_VECTOR_SET(tm, 1, scm_from_int(t->tm_min));
SCM_SIMPLE_VECTOR_SET(tm, 2, scm_from_int(t->tm_hour));
SCM_SIMPLE_VECTOR_SET(tm, 3, scm_from_int(t->tm_mday));
SCM_SIMPLE_VECTOR_SET(tm, 4, scm_from_int(t->tm_mon));
SCM_SIMPLE_VECTOR_SET(tm, 5, scm_from_int(t->tm_year));
SCM_SIMPLE_VECTOR_SET(tm, 6, scm_from_int(t->tm_wday));
SCM_SIMPLE_VECTOR_SET(tm, 7, scm_from_int(t->tm_yday));
SCM_SIMPLE_VECTOR_SET(tm, 8, scm_from_int(t->tm_isdst));
if (t == NULL)
{
SCM_SIMPLE_VECTOR_SET(tm, 0, scm_from_int(t->tm_sec));
SCM_SIMPLE_VECTOR_SET(tm, 1, scm_from_int(t->tm_min));
SCM_SIMPLE_VECTOR_SET(tm, 2, scm_from_int(t->tm_hour));
SCM_SIMPLE_VECTOR_SET(tm, 3, scm_from_int(t->tm_mday));
SCM_SIMPLE_VECTOR_SET(tm, 4, scm_from_int(t->tm_mon));
SCM_SIMPLE_VECTOR_SET(tm, 5, scm_from_int(t->tm_year));
SCM_SIMPLE_VECTOR_SET(tm, 6, scm_from_int(t->tm_wday));
SCM_SIMPLE_VECTOR_SET(tm, 7, scm_from_int(t->tm_yday));
SCM_SIMPLE_VECTOR_SET(tm, 8, scm_from_int(t->tm_isdst));
%#ifdef HAVE_STRUCT_TM_GMTOFF
SCM_SIMPLE_VECTOR_SET(tm, 9, scm_from_long(t->tm_gmtoff));
SCM_SIMPLE_VECTOR_SET(tm, 10, scm_from_locale_string(t->tm_zone?t->tm_zone:"Unset"));
SCM_SIMPLE_VECTOR_SET(tm, 9, scm_from_long(t->tm_gmtoff));
SCM_SIMPLE_VECTOR_SET(tm, 10, scm_from_locale_string(t->tm_zone?t->tm_zone:"Unset"));
%#else
SCM_SIMPLE_VECTOR_SET(tm, 9, scm_from_long(0));
SCM_SIMPLE_VECTOR_SET(tm, 10, scm_from_locale_string("GMT"));
SCM_SIMPLE_VECTOR_SET(tm, 9, scm_from_long(0));
SCM_SIMPLE_VECTOR_SET(tm, 10, scm_from_locale_string("GMT"));
%#endif
}
}
%define GLIST_HELPER_INOUT(ListType, ElemSwigType)

View File

@ -437,7 +437,8 @@ gcrd_time2dmy (time64 raw_time, gint *day, gint *month, gint *year)
struct tm * timeinfo;
timeinfo = gnc_localtime (&raw_time);
if (timeinfo == NULL)
return FALSE;
*day = timeinfo->tm_mday;
*month = timeinfo->tm_mon + 1;
*year = timeinfo->tm_year + 1900;

View File

@ -1091,11 +1091,14 @@ loan_info_page_save( GtkAssistant *assistant, gpointer user_data )
tmpTT = gnc_date_edit_get_date( ldd->prmStartDateGDE );
tmpTm = gnc_localtime ( &tmpTT );
g_date_set_dmy( ldd->ld.startDate,
tmpTm->tm_mday,
(tmpTm->tm_mon + 1),
(1900 + tmpTm->tm_year) );
gnc_tm_free (tmpTm);
if (tmpTm)
{
g_date_set_dmy( ldd->ld.startDate,
tmpTm->tm_mday,
(tmpTm->tm_mon + 1),
(1900 + tmpTm->tm_year) );
gnc_tm_free (tmpTm);
}
}
/* len / periods */

View File

@ -2426,10 +2426,13 @@ xaccTransGetDatePostedGDate (const Transaction *trans)
*/
time64 time = xaccTransGetDate(trans);
struct tm *stm = gnc_gmtime(&time);
g_date_set_dmy(&result, stm->tm_mday,
(GDateMonth)(stm->tm_mon + 1),
stm->tm_year + 1900);
free(stm);
if (stm)
{
g_date_set_dmy(&result, stm->tm_mday,
(GDateMonth)(stm->tm_mon + 1),
stm->tm_year + 1900);
free(stm);
}
}
}
return result;