diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2012-08-06 16:39:03 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2012-08-06 16:39:03 +0200 |
commit | 9e6bb604f1efaf491fa8998d8965b11669b1373c (patch) | |
tree | 3a573fcb9c441ce521f372fd631a9e280588142c /lib/gtk-experiment-widgets/gtk-experiment-transcript-formats.c | |
parent | 68e1ac4617108bb99b848bb1ccb1b55451f5b91b (diff) | |
download | experiment-player-9e6bb604f1efaf491fa8998d8965b11669b1373c.tar.gz |
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
Diffstat (limited to 'lib/gtk-experiment-widgets/gtk-experiment-transcript-formats.c')
-rw-r--r-- | lib/gtk-experiment-widgets/gtk-experiment-transcript-formats.c | 27 |
1 files changed, 20 insertions, 7 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; |