diff --git a/src/utils.c b/src/utils.c
index 9011b62dbf867a243f4350b8f9489cf41c8a61db..b8f3036ad29a8607517bfe26dfd9027e39ff22a1 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -109,7 +109,7 @@ write_file (const char *name, const char *content, size_t size)
   // if we don't make a copy, it will get free'd by the caller
   // before we write content to the file. The content should
   // be "small" (<1MiB): a Moodle web page
-  char *copy = g_strdup (content);
+  char *copy = g_strndup (content, size);
   g_file_replace_contents_async (file,
                                  copy,
                                  size,