From 9e6bb604f1efaf491fa8998d8965b11669b1373c Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 6 Aug 2012 16:39:03 +0200 Subject: when parsing a format file, check whether each line could be read completely, otherwise throw error * arbitrary limit (1024 bytes) per line - reading lines of arbritrary length is difficult and will be seldomly used * introduced is_newline() helper function --- .../gtk-experiment-transcript-formats.c | 27 ++++++++++++++++------ .../gtk-experiment-transcript-private.h | 7 ++++++ .../gtk-experiment-transcript.h | 3 ++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/gtk-experiment-widgets/gtk-experiment-transcript-formats.c b/lib/gtk-experiment-widgets/gtk-experiment-transcript-formats.c index c3dcbf8..040cb13 100644 --- a/lib/gtk-experiment-widgets/gtk-experiment-transcript-formats.c +++ b/lib/gtk-experiment-widgets/gtk-experiment-transcript-formats.c @@ -221,7 +221,7 @@ gtk_experiment_transcript_load_formats(GtkExperimentTranscript *trans, GError **error) { FILE *file; - gchar buf[255]; + gchar buf[1024]; gint cur_line = 0; gboolean res = FALSE; @@ -246,20 +246,33 @@ gtk_experiment_transcript_load_formats(GtkExperimentTranscript *trans, goto redraw; } - /** @bug will fail for lines longer than sizeof(buf)-1 */ while (fgets((char *)buf, sizeof(buf), file) != NULL) { GtkExperimentTranscriptFormat *fmt; cur_line++; + + if (!feof(file) && !is_newline(buf[strlen(buf) - 1])) { + g_set_error(error, + GTK_EXPERIMENT_TRANSCRIPT_ERROR, + GTK_EXPERIMENT_TRANSCRIPT_ERROR_LINELENGTH, + "Line %d in file \"%s\" is too long. " + "It must be less than %d characters.", + cur_line, filename, (int)sizeof(buf)); + + fclose(file); + gtk_experiment_transcript_free_formats(trans->priv->formats); + trans->priv->formats = NULL; + + goto redraw; + } + g_strchug(buf); /* strip new line chars from end of `buf' */ - for (gchar *p = buf + strlen(buf) - 1; p >= buf; p--) { - if (*p != '\r' && *p != '\n') - break; - + for (gchar *p = buf + strlen(buf) - 1; + p >= buf && is_newline(*p); + p--) *p = '\0'; - } if (*buf == '#' || *buf == '\0') continue; diff --git a/lib/gtk-experiment-widgets/gtk-experiment-transcript-private.h b/lib/gtk-experiment-widgets/gtk-experiment-transcript-private.h index 42cbc98..aaf7e37 100644 --- a/lib/gtk-experiment-widgets/gtk-experiment-transcript-private.h +++ b/lib/gtk-experiment-widgets/gtk-experiment-transcript-private.h @@ -130,4 +130,11 @@ gtk_experiment_transcript_free_format(GtkExperimentTranscriptFormat *format) G_GNUC_INTERNAL void gtk_experiment_transcript_free_formats(GSList *formats); +/** @private */ +static inline gboolean +is_newline(gchar c) +{ + return c == '\r' || c == '\n'; +} + #endif diff --git a/lib/gtk-experiment-widgets/gtk-experiment-transcript.h b/lib/gtk-experiment-widgets/gtk-experiment-transcript.h index 7def2ef..7fb1ba4 100644 --- a/lib/gtk-experiment-widgets/gtk-experiment-transcript.h +++ b/lib/gtk-experiment-widgets/gtk-experiment-transcript.h @@ -41,7 +41,8 @@ G_BEGIN_DECLS /** \e GtkExperimentTranscript error codes */ typedef enum { GTK_EXPERIMENT_TRANSCRIPT_ERROR_FILEOPEN, /**< Error opening file */ - GTK_EXPERIMENT_TRANSCRIPT_ERROR_REGEXCAPTURES /**< Additional regular expression captures used */ + GTK_EXPERIMENT_TRANSCRIPT_ERROR_REGEXCAPTURES, /**< Additional regular expression captures used */ + GTK_EXPERIMENT_TRANSCRIPT_ERROR_LINELENGTH /**< Line read is too long */ } GtkExperimentTranscriptError; /** \e GtkExperimentTranscript type */ -- cgit v1.2.3