From: Peter Verthez Date: Sun, 8 Sep 2002 19:12:43 +0000 (+0000) Subject: More thorough error checking of library calls. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=4af0e99da018aa2eb898de1e1dbc10839b857253;p=gedcom-parse.git More thorough error checking of library calls. --- diff --git a/gedcom/multilex.c b/gedcom/multilex.c index 96af295..ada4ff0 100644 --- a/gedcom/multilex.c +++ b/gedcom/multilex.c @@ -69,17 +69,24 @@ int gedcom_lex() int determine_encoding(FILE* f) { char first[2]; + int read; - fread(first, 1, 2, f); - if ((first[0] == '0') && (first[1] == ' ')) { + read = fread(first, 1, 2, f); + if (read != 2) { + gedcom_warning(_("Error reading from input file: %s"), strerror(errno)); + return ONE_BYTE; + } + else if ((first[0] == '0') && (first[1] == ' ')) { gedcom_debug_print(_("One-byte encoding")); - fseek(f, 0, 0); + if (fseek(f, 0, 0) != 0) + gedcom_warning(_("Error positioning input file: %s"), strerror(errno)); return ONE_BYTE; } else if ((first[0] == '\0') && (first[1] == '0')) { gedcom_debug_print(_("Two-byte encoding, high-low")); - fseek(f, 0, 0); + if (fseek(f, 0, 0) != 0) + gedcom_warning(_("Error positioning input file: %s"), strerror(errno)); return TWO_BYTE_HILO; } else if ((first[0] == '\xFE') && (first[1] == '\xFF')) @@ -90,7 +97,8 @@ int determine_encoding(FILE* f) else if ((first[0] == '0') && (first[1] == '\0')) { gedcom_debug_print(_("Two-byte encoding, low-high")); - fseek(f, 0, 0); + if (fseek(f, 0, 0) != 0) + gedcom_warning(_("Error positioning input file: %s"), strerror(errno)); return TWO_BYTE_LOHI; } else if ((first[0] == '\xFF') && (first[1] == '\xFE')) @@ -100,7 +108,8 @@ int determine_encoding(FILE* f) } else { gedcom_warning(_("Unknown encoding, falling back to one-byte")); - fseek(f, 0, 0); + if (fseek(f, 0, 0) != 0) + gedcom_warning(_("Error positioning input file: %s"), strerror(errno)); return ONE_BYTE; } } @@ -118,13 +127,33 @@ int gedcom_parse_file(char* file_name) ENCODING enc; int result = 1; FILE* file; + char *locale, *save_locale, *save_textdom; + + locale = setlocale(LC_ALL, NULL); + if (! locale) { + gedcom_error(_("Could not retrieve locale information")); + return result; + } + + save_locale = strdup(locale); + if (! save_locale) { + MEMORY_ERROR; + return result; + } + + save_textdom = textdomain(NULL); + if (!save_textdom) { + gedcom_error(_("Could not retrieve locale domain: %s"), strerror(errno)); + return result; + } - char *save_locale = strdup(setlocale(LC_ALL, NULL)); - char *save_textdom = textdomain(NULL); - setlocale(LC_ALL, ""); - bindtextdomain(PACKAGE, LOCALEDIR); - bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING); - textdomain(PACKAGE); + if (! setlocale(LC_ALL, "") + || ! bindtextdomain(PACKAGE, LOCALEDIR) + || ! bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING) + || ! textdomain(PACKAGE)) { + gedcom_error(_("Could not set locale: %s"), strerror(errno)); + return result; + } if (!init_called) { gedcom_error(_("Internal error: GEDCOM parser not initialized")); @@ -133,7 +162,8 @@ int gedcom_parse_file(char* file_name) line_no = 1; file = fopen(file_name, "r"); if (!file) { - gedcom_error(_("Could not open file '%s'"), file_name); + gedcom_error(_("Could not open file '%s': %s"), + file_name, strerror(errno)); } else { init_encodings(); @@ -151,8 +181,11 @@ int gedcom_parse_file(char* file_name) } } - textdomain(save_textdom); - setlocale(LC_ALL, save_locale); + if (! textdomain(save_textdom) + || ! setlocale(LC_ALL, save_locale)) { + gedcom_error(_("Could not restore locale: %s"), strerror(errno)); + return result; + } free(save_locale); return result; } diff --git a/gedcom/xref.c b/gedcom/xref.c index ee58ad0..bc05ac3 100644 --- a/gedcom/xref.c +++ b/gedcom/xref.c @@ -68,6 +68,7 @@ void clear_xref_node(struct xref_node *xr) /* Make sure that the 'string' member always contains a valid string */ if (!xr->xref.string) xr->xref.string = strdup(""); + if (!xr->xref.string) MEMORY_ERROR; xr->xref.object = NULL; xr->defined_type = XREF_NONE; xr->used_type = XREF_NONE; @@ -78,8 +79,12 @@ void clear_xref_node(struct xref_node *xr) struct xref_node *make_xref_node() { struct xref_node *xr = (struct xref_node *)malloc(sizeof(struct xref_node)); - xr->xref.string = NULL; - clear_xref_node(xr); + if (xr) { + xr->xref.string = NULL; + clear_xref_node(xr); + } + else + MEMORY_ERROR; return xr; } @@ -96,7 +101,9 @@ void make_xref_table() else /* Only register initially (if xrefs is still NULL) */ /* So that it is only registered once */ - atexit(cleanup_xrefs); + if (atexit(cleanup_xrefs) != 0) { + gedcom_warning(_("Could not register xref cleanup function")); + } xrefs = hash_create(HASHCOUNT_T_MAX, NULL, NULL); hash_set_allocator(xrefs, xref_alloc, xref_free, NULL); } @@ -148,11 +155,17 @@ struct xref_value *gedcom_parse_xref(char *raw_value, } else { char *key = strdup(raw_value); - xr = make_xref_node(); - xr->xref.type = xref_type; - free(xr->xref.string); - xr->xref.string = strdup(raw_value); - hash_alloc_insert(xrefs, key, xr); + if (key) { + xr = make_xref_node(); + xr->xref.type = xref_type; + if (xr->xref.string) + free(xr->xref.string); + xr->xref.string = strdup(raw_value); + if (! xr->xref.string) MEMORY_ERROR; + hash_alloc_insert(xrefs, key, xr); + } + else + MEMORY_ERROR; } if (ctxt == XREF_DEFINED && xr->defined_type == XREF_NONE) {