Skip to content

Commit

Permalink
pluma-document: Fix: don't crash using files with 'bom'
Browse files Browse the repository at this point in the history
Fixes #301
  • Loading branch information
sc0w authored and lukefromdc committed Aug 23, 2018
1 parent bc64980 commit 4a74dc6
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion pluma/pluma-document.c
Expand Up @@ -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);

Expand All @@ -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),
Expand Down

0 comments on commit 4a74dc6

Please sign in to comment.