diff options
author | Pablo Barciela <[email protected]> | 2018-08-19 05:38:15 +0200 |
---|---|---|
committer | lukefromdc <[email protected]> | 2018-08-23 02:01:05 -0400 |
commit | 4a74dc65ffb89e0ef391cdf1f0d4c07cb1d20889 (patch) | |
tree | 61df3de377617f03122ef5dd3b7cb3332de190d6 | |
parent | bc649809762e5ae9c66cf53a1e2abcf2f4534663 (diff) | |
download | pluma-4a74dc65ffb89e0ef391cdf1f0d4c07cb1d20889.tar.bz2 pluma-4a74dc65ffb89e0ef391cdf1f0d4c07cb1d20889.tar.xz |
pluma-document: Fix: don't crash using files with 'bom'
Fixes https://github.com/mate-desktop/pluma/issues/301
-rw-r--r-- | pluma/pluma-document.c | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/pluma/pluma-document.c b/pluma/pluma-document.c index 9364ef1d..9cc305d8 100644 --- a/pluma/pluma-document.c +++ b/pluma/pluma-document.c @@ -650,12 +650,58 @@ pluma_document_class_init (PlumaDocumentClass *klass) g_type_class_add_private (object_class, sizeof(PlumaDocumentPrivate)); } +static gboolean +file_with_bom (GFile *file) +{ + FILE *testfile; + gchar c; + int i; + gchar *bom; + gchar *file_path; + gboolean has_bom; + + file_path = g_file_get_path (file); + + testfile = fopen (file_path, "r"); + + g_free (file_path); + + if (testfile == NULL) + { + perror ("fopen"); + return FALSE; + } + + bom = ""; + + for (i = 0; i < 3; i++) + { + c = fgetc (testfile); + + if (c == EOF) + break; + else + bom = g_strdup_printf ("%s%c", bom, c); + } + + fclose (testfile); + + if (g_strcmp0 (bom, "\357\273\277") == 0) + has_bom = TRUE; + else + has_bom = FALSE; + + g_free (bom); + return has_bom; +} + static void set_language (PlumaDocument *doc, GtkSourceLanguage *lang, gboolean set_by_user) { GtkSourceLanguage *old_lang; + const gchar *bom_langs; pluma_debug (DEBUG_DOCUMENT); @@ -664,7 +710,20 @@ set_language (PlumaDocument *doc, if (old_lang == lang) return; - gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (doc), lang); + bom_langs = "asp,dtl,docbook,html,mxml,mallard,markdown,mediawiki,php,tera,xml,xslt"; + + if (g_strrstr (bom_langs, gtk_source_language_get_id (lang))) + { + GFile *file; + file = pluma_document_get_location (doc); + + if (!file_with_bom (file)) + gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (doc), lang); + + g_object_unref (file); + } + else + gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (doc), lang); if (lang != NULL) gtk_source_buffer_set_highlight_syntax (GTK_SOURCE_BUFFER (doc), |