]> git.draconx.ca Git - liblbx.git/commitdiff
lbximg: Fix management of external palette file handles.
authorNick Bowler <nbowler@draconx.ca>
Tue, 28 Jan 2014 01:03:34 +0000 (20:03 -0500)
committerNick Bowler <nbowler@draconx.ca>
Tue, 28 Jan 2014 01:37:33 +0000 (20:37 -0500)
We leak file handles for the external and override palettes because the
file is opened every time the --palette or --override options are set.
Fix that up, and also close the handles when finished.

src/lbximg.c

index 26086892ca97ec77613a0e0af38f45199c1d3bf1..dc4697b31568c0f6cb7483f7fca3aabdb4b5ab25 100644 (file)
@@ -437,6 +437,7 @@ int main(int argc, char **argv)
        int mode = MODE_NONE, fmt, opt, rc = EXIT_FAILURE;
        struct lbx_pipe_state stdin_handle = { .f = stdin };
        const char *file = NULL, *fmtstring = NULL;
+       const char *ext_palette = NULL, *ovr_palette = NULL;
        FILE *palf = NULL, *overf = NULL;
        struct lbx_image *img;
 
@@ -482,19 +483,11 @@ int main(int argc, char **argv)
                        usepalette = 0;
                        break;
                case 'p':
-                       palf = fopen(optarg, "rb");
-                       if (!palf) {
-                               tool_err(0, "failed to open %s", optarg);
-                               return EXIT_FAILURE;
-                       }
+                       ext_palette = optarg;
 
                        break;
                case 'O':
-                       overf = fopen(optarg, "rb");
-                       if (!overf) {
-                               tool_err(0, "failed to open %s", optarg);
-                               return EXIT_FAILURE;
-                       }
+                       ovr_palette = optarg;
                        break;
                case OPT_PREFIX:
                        outname = optarg;
@@ -532,6 +525,16 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
+       if (ext_palette && !(palf = fopen(ext_palette, "rb"))) {
+               tool_err(0, "failed to open %s", optarg);
+               return EXIT_FAILURE;
+       }
+
+       if (ovr_palette && !(overf = fopen(ovr_palette, "rb"))) {
+               tool_err(0, "failed to open %s", optarg);
+               return EXIT_FAILURE;
+       }
+
        if (verbose || mode == MODE_IDENT) {
                struct lbx_imginfo info;
 
@@ -556,5 +559,9 @@ int main(int argc, char **argv)
        }
 
        lbx_img_close(img);
+       if (palf)
+               fclose(palf);
+       if (overf)
+               fclose(overf);
        return rc;
 }