]> git.draconx.ca Git - liblbx.git/commitdiff
liblbx: Properly report errors in lbx_(img_)fopen.
authorNick Bowler <nbowler@draconx.ca>
Wed, 12 Jun 2013 01:07:30 +0000 (21:07 -0400)
committerNick Bowler <nbowler@draconx.ca>
Fri, 14 Jun 2013 20:35:08 +0000 (16:35 -0400)
The lbx_fopen and lbx_img_fopen functions were not properly pushing an
error code when they failed, leading to extremely helpful messages of
the form:

  lbximg: failed to open image: Success.

It further turns out that pushing negative errors (i.e., -errno) was
documented but never actually tested or used, so fix that up, too.
It is likely that many other sites are lacking appropriate calls to
lbx_error_raise, which can be added later.

src/error.c
src/image.c
src/lbx.c

index c32d3b6188e7afb208fb83fa24fdbe38f90292a6..3e47204c76716ea6cd075a80e3423420af77270f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  2ooM: The Master of Orion II Reverse Engineering Project
  *  Utilities for out-of-band error propagation.
 /*
  *  2ooM: The Master of Orion II Reverse Engineering Project
  *  Utilities for out-of-band error propagation.
- *  Copyright (C) 2010 Nick Bowler
+ *  Copyright © 2010, 2013 Nick Bowler
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -22,6 +22,8 @@
 #include <errno.h>
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
 #include <string.h>
 #include <assert.h>
+#include <limits.h>
+#include <inttypes.h>
 #include <libintl.h>
 
 #include "error.h"
 #include <libintl.h>
 
 #include "error.h"
@@ -36,16 +38,24 @@ static unsigned error_base, error_tip;
 static int error_ring[LBX_ERROR_LIMIT];
 
 static const char **user_errors;
 static int error_ring[LBX_ERROR_LIMIT];
 
 static const char **user_errors;
-static unsigned user_error_count;
-static unsigned user_error_max = 2;
+static int user_error_count, user_error_max = 2;
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
 
 int lbx_error_new(const char *str)
 {
 
 int lbx_error_new(const char *str)
 {
-       assert(user_error_count <= user_error_max);
+       assert(user_error_count >= 0 && user_error_count <= user_error_max);
+
        if (!user_errors || user_error_count == user_error_max) {
        if (!user_errors || user_error_count == user_error_max) {
-               size_t size = sizeof *user_errors * user_error_max * 2;
                const char **new;
                const char **new;
+               size_t size;
+
+               if (user_error_max >= MIN(SIZE_MAX, INT_MAX)/2 - LBX_EUBASE)
+                       return -1;
+               if (2 * user_error_max >= SIZE_MAX / sizeof *user_errors)
+                       return -1;
 
 
+               size = 2 * user_error_max * sizeof *user_errors;
                new = realloc(user_errors, size);
                if (!new)
                        return -1;
                new = realloc(user_errors, size);
                if (!new)
                        return -1;
@@ -61,8 +71,8 @@ int lbx_error_raise(int code)
 {
        if (code == LBX_EOK) {
                return -1;
 {
        if (code == LBX_EOK) {
                return -1;
-       } else if (code >= LBX_EMAX && code < LBX_EUBASE
-                  || code >= LBX_EUBASE + user_error_count) {
+       } else if (code >= 0 && ((code >= LBX_EMAX && code < LBX_EUBASE)
+                                 || code >= LBX_EUBASE + user_error_count)) {
                fprintf(stderr, "%s: invalid error code %d\n", __func__, code);
                return -1;
        }
                fprintf(stderr, "%s: invalid error code %d\n", __func__, code);
                return -1;
        }
index f407a2d72d594157f4d05b353364c5720baa8f9d..4657208a05ae28439496346473c8de7cb1566023 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * 2ooM: The Master of Orion II Reverse Engineering Project
  * Library for working with LBX image files.
 /*
  * 2ooM: The Master of Orion II Reverse Engineering Project
  * Library for working with LBX image files.
- * Copyright © 2006-2011 Nick Bowler
+ * Copyright © 2006-2011, 2013 Nick Bowler
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -176,14 +176,17 @@ struct lbx_image *lbx_img_fopen(const char *file)
        FILE *f;
 
        f = fopen(file, "rb");
        FILE *f;
 
        f = fopen(file, "rb");
-       if (!f)
+       if (!f) {
+               lbx_error_raise(-errno);
                return NULL;
                return NULL;
+       }
 
        if (fseek(f, 0, SEEK_CUR) == 0)
                return lbx_img_open(f, &lbx_default_fops, file_close);
 
        p = malloc(sizeof *p);
        if (!p) {
 
        if (fseek(f, 0, SEEK_CUR) == 0)
                return lbx_img_open(f, &lbx_default_fops, file_close);
 
        p = malloc(sizeof *p);
        if (!p) {
+               lbx_error_raise(LBX_ENOMEM);
                fclose(f);
                return NULL;
        }
                fclose(f);
                return NULL;
        }
index 57a72b082e70158ff8abd6d2205f90cce4f13caa..a2d37961a77d33b95d8761a5a1216913ce4e9d33 100644 (file)
--- a/src/lbx.c
+++ b/src/lbx.c
@@ -1,7 +1,7 @@
 /*
  *  2ooM: The Master of Orion II Reverse Engineering Project
  *  Library for working with LBX archive files.
 /*
  *  2ooM: The Master of Orion II Reverse Engineering Project
  *  Library for working with LBX archive files.
- *  Copyright (C) 2006-2010 Nick Bowler
+ *  Copyright © 2006-2010, 2013 Nick Bowler
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -166,14 +166,17 @@ struct lbx *lbx_fopen(const char *file)
        FILE *f;
 
        f = fopen(file, "rb");
        FILE *f;
 
        f = fopen(file, "rb");
-       if (!f)
+       if (!f) {
+               lbx_error_raise(-errno);
                return NULL;
                return NULL;
+       }
 
        if (fseek(f, 0, SEEK_CUR) == 0)
                return lbx_open(f, &lbx_default_fops, file_close, name);
 
        p = malloc(sizeof *p);
        if (!p) {
 
        if (fseek(f, 0, SEEK_CUR) == 0)
                return lbx_open(f, &lbx_default_fops, file_close, name);
 
        p = malloc(sizeof *p);
        if (!p) {
+               lbx_error_raise(LBX_ENOMEM);
                fclose(f);
                return NULL;
        }
                fclose(f);
                return NULL;
        }