Hello,

Ying-Chun Liu (PaulLiu), le Sun 20 Nov 2011 14:58:30 +0800, a écrit :
> Can you help me to review the patch for mx?

Here is a revised version of the patch, using g_strdup_printf instead
of computing the size in advance, thus making it a lot more obviously
correct.

Samuel
--- mx/mx-create-image-cache.c.original 2012-01-06 00:07:01.000000000 +0000
+++ mx/mx-create-image-cache.c  2012-01-06 00:25:36.000000000 +0000
@@ -417,7 +417,7 @@
                              char *pngfile)
 {
   FILE *file;
-  char filename[PATH_MAX *2];
+  char *filename = NULL;
   struct imgcache_element element;
   struct imgcache_element *elm;
   GList *item;
@@ -426,11 +426,12 @@
 
   strcpy(&element.filename[0], pngfile);
 
-  sprintf(filename, "%s/mx.cache", directory);
+  filename = g_strdup_printf("%s/mx.cache", directory);
 
   file = fopen(filename, "w");
   if (!file) {
       fprintf(stderr, "Cannot write cache file: %s\n", filename);
+      g_free (filename);
       return;
     }
   fwrite(&element, 1, sizeof(element), file);
@@ -442,6 +443,7 @@
       elm->ptr = NULL;
       fwrite(elm, 1, sizeof(element), file);
     }
+  g_free (filename);
   fclose(file);
 
 }
@@ -449,7 +451,8 @@
 int main(int    argc,
          char **argv)
 {
-  char image_file[PATH_MAX];
+  char *image_file = NULL;
+
   if (argc <= 1) {
       printf("Usage:\n\t\tmakecache <directory>\n");
       return EXIT_FAILURE;
@@ -457,8 +460,9 @@
   g_type_init();
   makecache(argv[1], 1);
   optimal_placement();
-  sprintf(image_file, "/var/cache/mx/%08x.png", g_str_hash(argv[1]));
+  image_file = g_strdup_printf("/var/cache/mx/%08x.png", g_str_hash(argv[1]));
   if (make_final_image(image_file))
     write_cache_file(argv[1], image_file);
+  g_free (image_file);
   return EXIT_SUCCESS;
 }

Reply via email to