groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 01/01: `preconv': fix usage of lib `uchardet'.


From: Bertrand Garrigues
Subject: [groff] 01/01: `preconv': fix usage of lib `uchardet'.
Date: Sun, 5 Nov 2017 16:05:27 -0500 (EST)

bgarrigues pushed a commit to branch master
in repository groff.

commit 305988c2b205b077bfdec961632bd503c0858f20
Author: Bertrand Garrigues <address@hidden>
Date:   Sun Nov 5 21:55:41 2017 +0100

    `preconv': fix usage of lib `uchardet'.
    
    * src/preproc/preconv/preconv.cpp (detect_file_encoding): Fix
    usage of uchardet 0.0.1, which may fail but return an empty string
    instead of a null pointer, and fix some incorrect error
    managements that might cause memory leaks.
---
 ChangeLog                       |  9 +++++++++
 src/preproc/preconv/preconv.cpp | 38 +++++++++++++++++++++++---------------
 2 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d2542be..01be7e3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2017-11-05  Bertrand Garrigues <address@hidden>
+
+       `preconv': fix usage of lib `uchardet'.
+
+       * src/preproc/preconv/preconv.cpp (detect_file_encoding): Fix
+       usage of uchardet 0.0.1, which may fail but return an empty string
+       instead of a null pointer, and fix some incorrect error
+       managements that might cause memory leaks.
+
 2017-11-05  G. Branden Robinson <address@hidden>
 
        font/devpdf/devpdf.am: Silence warning from grep during clean.
diff --git a/src/preproc/preconv/preconv.cpp b/src/preproc/preconv/preconv.cpp
index 97d4feb..96ae7f8 100644
--- a/src/preproc/preconv/preconv.cpp
+++ b/src/preproc/preconv/preconv.cpp
@@ -1005,56 +1005,64 @@ char *
 detect_file_encoding(FILE *fp)
 {
 #ifdef HAVE_UCHARDET
-  uchardet_t ud;
+  uchardet_t ud = NULL;
   struct stat stat_buf;
   size_t len, read_bytes;
-  char *data;
+  char *data = NULL;
   int res, current_position;
   const char *charset;
-  char *ret;
+  char *ret = NULL;
 
   current_position = ftell(fp);
   /* due to BOM and tag detection we are not at the begining of the file */
   rewind(fp);
   if (fstat(fileno(fp), &stat_buf) != 0) {
     fprintf(stderr, "fstat: %s\n", strerror(errno));
-    return NULL;
+    goto end;
   }
   len = stat_buf.st_size;
   if (debug_flag)
     fprintf(stderr, "  len: %zu\n", len);  
   if (len == 0)
-    return NULL;
+    goto end;
   data = (char *)calloc(len, 1);
   read_bytes = fread(data, 1, len, fp);
   if (read_bytes == 0) {
     fprintf(stderr, "fread: %s\n", strerror(errno));
-    return NULL;
+    goto end;
   }
   /* We rewind back to the original position */
   if (fseek(fp, current_position, SEEK_SET) != 0) {
     fprintf(stderr, "Fatal error: fseek: %s\n", strerror(errno));
-    return NULL;
+    goto end;
   }
   ud = uchardet_new();
   res = uchardet_handle_data(ud, data, len);
   if (res != 0) {
     fprintf(stderr, "uchardet_handle_data: %d\n", res);
-    uchardet_delete(ud);
-    return NULL;
+    goto end;
   }
   if (debug_flag)
     fprintf(stderr, "  uchardet read: %zu bytes\n", read_bytes);
   uchardet_data_end(ud);
   charset = uchardet_get_charset(ud);
-  if (debug_flag)
-    fprintf(stderr, "  charset: %s\n", charset);
-  if (charset) {
-    ret = (char *)calloc(strlen(charset) + 1, 1);
+  if (debug_flag) {
+    if (charset)
+       fprintf(stderr, "  charset: %s\n", charset);
+    else
+       fprintf(stderr, "  charset is NULL\n");
+  }
+  /* uchardet 0.0.1 could return an empty string instead of NULL */
+  if (charset && *charset) {
+    ret = (char *)malloc(strlen(charset) + 1);
     strcpy(ret, charset);
   }
-  uchardet_delete(ud);
-  free(data);
+
+end:
+  if (ud)
+     uchardet_delete(ud);
+  if (data)
+     free(data);
 
   return ret;
 #else /* not HAVE_UCHARDET */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]