GNOME Bugzilla – Bug 357945
gtk_calendar_select_day does not validate the day properly
Last modified: 2018-02-10 03:36:46 UTC
Please describe the problem: It is possible to set dates like 30th of February or 31th of June. This happens becasue the only check done in gtk_calendar_select_day is whether the day is lower than or equal to 31. Steps to reproduce: #include <gtk/gtk.h> int main () { GtkCalendar *calendar; guint year = 2006; guint month = 1; guint day = 30; gtk_init (0, NULL); calendar = GTK_CALENDAR (gtk_calendar_new ()); gtk_calendar_select_month (calendar, month, year); gtk_calendar_select_day (calendar, day); gtk_calendar_get_date (calendar, &year, &month, &day); g_print ("\nYear=%d, month=%d, day=%d\n", year, month, day); gtk_widget_destroy (GTK_WIDGET (calendar)); return 0; } Actual results: Year=2006, month=1, day=30 Expected results: The day should be clamped to 28 or not being modified at all. Does this happen every time? Yes Other information:
Created attachment 73547 [details] [review] Fixes the problem checking the day number against the month_length array
The patch changes documented behavior so the api documentation needs to be updated. Then what should be done when a valid date is selected for the specified month and the month is then changed? // Goto 20080131 gtk_calendar_select_month (cal, 0, 2008); gtk_calendar_select_day (cal, 31); // Goto 20080231 ... Ooops! gtk_calendar_select_month (cal, 1, 2008); gtk_calendar_mark_day() and gtk_calendar_unmark_day() probably also should be updated to guard against invalid dates.
I would suggest an atomic function which sets day, month, and year in one operation. If necessary, fields with -1 could be ignored to mimic the actual select functions: gtk_calendar_set_date(GtkCalendar *calendar, guint day, guint month, guint year); This way, a completer validation can be done - if setting the day causes a conflict, set month/year first then check day again. Else the problem exists two ways: calendar_invalidate_day_num runs through the days of the current month, and check if the requested day exists. So, if I'm in February, and set the day to (29,) 30, 31, I get an error, if I understand this correctly. But an error also occurs if I'm on March, 31th, and set the month/year first to February. John
We're moving to gitlab! As part of this move, we are closing bugs that haven't seen activity in more than 5 years. If this issue is still imporant to you and still relevant with GTK+ 3.22 or master, please consider creating a gitlab issue for it.