aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2012-08-06 16:39:03 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2012-08-06 16:39:03 +0200
commit9e6bb604f1efaf491fa8998d8965b11669b1373c (patch)
tree3a573fcb9c441ce521f372fd631a9e280588142c
parent68e1ac4617108bb99b848bb1ccb1b55451f5b91b (diff)
downloadexperiment-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
-rw-r--r--lib/gtk-experiment-widgets/gtk-experiment-transcript-formats.c27
-rw-r--r--lib/gtk-experiment-widgets/gtk-experiment-transcript-private.h7
-rw-r--r--lib/gtk-experiment-widgets/gtk-experiment-transcript.h3
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 */