[ghex] Cleanup and fix memory leak in hex-buffer-mmap



commit 7748633fcf66e808584e8d5ee13bf08ea04a6da6
Author: Logan Rathbone <poprocks gmail com>
Date:   Sun Dec 19 21:08:02 2021 -0500

    Cleanup and fix memory leak in hex-buffer-mmap

 src/findreplace.c     |  5 ++--
 src/hex-buffer-mmap.c |  9 ++++----
 src/hex-document.c    | 34 ++++++++++++++-------------
 src/hex-document.h    | 64 +++++++++++++++++++++++++++++----------------------
 4 files changed, 62 insertions(+), 50 deletions(-)
---
diff --git a/src/findreplace.c b/src/findreplace.c
index d0996ee..cd3f26a 100644
--- a/src/findreplace.c
+++ b/src/findreplace.c
@@ -203,7 +203,8 @@ find_common (FindDialog *self, enum FindDirection direction,
        doc = gtk_hex_get_document (priv->gh);
        cursor_pos = gtk_hex_get_cursor (priv->gh);
 
-       if ((str_len = get_search_string (f_priv->f_doc, &str)) == 0)
+       str_len = get_search_string (f_priv->f_doc, &str);
+       if (str_len == 0)
        {
                no_string_dialog (parent);
                return;
@@ -215,7 +216,7 @@ find_common (FindDialog *self, enum FindDirection direction,
                gtk_hex_delete_autohighlight (priv->gh, priv->auto_highlight);
 
        priv->auto_highlight = NULL;
-       priv->auto_highlight = gtk_hex_insert_autohighlight(priv->gh,
+       priv->auto_highlight = gtk_hex_insert_autohighlight (priv->gh,
                        str, str_len);
 
        /* Search for requested string */
diff --git a/src/hex-buffer-mmap.c b/src/hex-buffer-mmap.c
index 0c8eb4c..d6c6fc0 100644
--- a/src/hex-buffer-mmap.c
+++ b/src/hex-buffer-mmap.c
@@ -407,12 +407,11 @@ char hex_buffer_mmap_get_byte (HexBuffer *buf,
 {
        HexBufferMmap *self = HEX_BUFFER_MMAP (buf);
        char *cp;
-       char c;
 
-       cp = hex_buffer_mmap_get_data (buf, offset, 1);
-       c = *cp;
-       
-       return c;
+       if (hex_buffer_mmap_raw (self, &cp, offset, 1))
+               return cp[0];
+       else
+               return 0;
 }
 
 static gint64
diff --git a/src/hex-document.c b/src/hex-document.c
index ffbab45..cc66942 100644
--- a/src/hex-document.c
+++ b/src/hex-document.c
@@ -459,7 +459,8 @@ hex_document_set_data (HexDocument *doc, gint64 offset, size_t len,
 }
 
 void
-hex_document_delete_data(HexDocument *doc, guint offset, guint len, gboolean undoable)
+hex_document_delete_data (HexDocument *doc,
+               gint64 offset, size_t len, gboolean undoable)
 {
        hex_document_set_data (doc, offset, 0, len, NULL, undoable);
 }
@@ -553,7 +554,7 @@ hex_document_has_changed(HexDocument *doc)
 }
 
 void
-hex_document_set_max_undo(HexDocument *doc, int max_undo)
+hex_document_set_max_undo (HexDocument *doc, int max_undo)
 {
        if(doc->undo_max != max_undo) {
                if(doc->undo_max > max_undo)
@@ -725,16 +726,19 @@ hex_document_export_html (HexDocument *doc, char *html_path, char *base_name,
 }
 
 int
-hex_document_compare_data(HexDocument *doc, char *s2, int pos, int len)
+hex_document_compare_data (HexDocument *doc,
+               char *what, gint64 pos, size_t len)
 {
-       char c1;
+       char c;
 
-       for (int i = 0; i < len; i++, s2++)
+       g_return_val_if_fail (what, 0);
+
+       for (size_t i = 0; i < len; i++, what++)
        {
-               c1 = hex_buffer_get_byte (doc->buffer, pos + i);
+               c = hex_buffer_get_byte (doc->buffer, pos + i);
 
-               if(c1 != (*s2))
-                       return (c1 - (*s2));
+               if (c != *what)
+                       return (c - *what);
        }
        
        return 0;
@@ -751,7 +755,7 @@ hex_document_find_forward (HexDocument *doc, gint64 start, char *what,
        pos = start;
        while (pos < payload)
        {
-               if (hex_document_compare_data(doc, what, pos, len) == 0)
+               if (hex_document_compare_data (doc, what, pos, len) == 0)
                {
                        *found = pos;
                        return TRUE;
@@ -766,20 +770,18 @@ gboolean
 hex_document_find_backward (HexDocument *doc, gint64 start, char *what,
                                                   size_t len, gint64 *found)
 {
-       gint64 pos;
+       gint64 pos = start;
        
-       pos = start;
-
-       if(pos == 0)
+       if (pos == 0)
                return FALSE;
 
        do {
                pos--;
-               if(hex_document_compare_data(doc, what, pos, len) == 0) {
+               if (hex_document_compare_data (doc, what, pos, len) == 0) {
                        *found = pos;
                        return TRUE;
                }
-       } while(pos > 0);
+       } while (pos > 0);
 
        return FALSE;
 }
@@ -843,7 +845,7 @@ hex_document_real_undo (HexDocument *doc)
 }
 
 gboolean 
-hex_document_redo(HexDocument *doc)
+hex_document_redo (HexDocument *doc)
 {
        if(doc->undo_stack == NULL || doc->undo_top == doc->undo_stack)
                return FALSE;
diff --git a/src/hex-document.h b/src/hex-document.h
index a5921b5..71326bf 100644
--- a/src/hex-document.h
+++ b/src/hex-document.h
@@ -44,7 +44,8 @@ G_BEGIN_DECLS
 #define HEX_TYPE_DOCUMENT hex_document_get_type ()
 G_DECLARE_FINAL_TYPE (HexDocument, hex_document, HEX, DOCUMENT, GObject)
 
-typedef enum {
+typedef enum
+{
        HEX_CHANGE_STRING,
        HEX_CHANGE_BYTE
 } HexChangeType;
@@ -53,7 +54,8 @@ typedef struct _HexChangeData HexChangeData;
 struct _HexChangeData
 {
        gint64 start, end;
-       /* length to replace (overwrite); (0 to insert without overwriting) */
+       /* `replace length`: length to replace (overwrite); (0 to insert without
+        * overwriting) */
        size_t rep_len;
        gboolean lower_nibble;
        gboolean insert;
@@ -63,33 +65,41 @@ struct _HexChangeData
 };
 
 
-HexDocument *hex_document_new(void);
-HexDocument *hex_document_new_from_file (GFile *file);
-void        hex_document_set_data(HexDocument *doc, gint64 offset, size_t len, size_t rep_len, char *data, 
gboolean undoable);
-void        hex_document_set_byte(HexDocument *doc, char val, gint64 offset, gboolean insert, gboolean 
undoable);
-void        hex_document_set_nibble(HexDocument *doc, char val, gint64 offset, gboolean lower_nibble, 
gboolean insert, gboolean undoable);
-void        hex_document_delete_data(HexDocument *doc, guint offset, guint len, gboolean undoable);
+HexDocument    *hex_document_new(void);
+HexDocument    *hex_document_new_from_file (GFile *file);
+void           hex_document_set_data (HexDocument *doc, gint64 offset, size_t len,
+               size_t rep_len, char *data, gboolean undoable);
+void           hex_document_set_byte (HexDocument *doc, char val, gint64 offset,
+               gboolean insert, gboolean undoable);
+void           hex_document_set_nibble (HexDocument *doc, char val, gint64 offset,
+               gboolean lower_nibble, gboolean insert, gboolean undoable);
+void           hex_document_delete_data (HexDocument *doc, gint64 offset, 
+               size_t len, gboolean undoable);
 void           hex_document_read (HexDocument *doc);
-gboolean   hex_document_write(HexDocument *doc);
-
-gboolean   hex_document_write_to_file (HexDocument *doc, GFile *file);
-gboolean    hex_document_export_html (HexDocument *doc, char *html_path, char *base_name, gint64 start, 
gint64 end, guint cpl, guint lpp, guint cpw);
-gboolean    hex_document_has_changed(HexDocument *doc);
-void        hex_document_changed(HexDocument *doc, gpointer change_data, gboolean push_undo);
-void        hex_document_set_max_undo(HexDocument *doc, int max_undo);
-gboolean    hex_document_undo(HexDocument *doc);
-gboolean    hex_document_redo(HexDocument *doc);
-int        hex_document_compare_data(HexDocument *doc, char *s2, int pos, int len);
-gboolean   hex_document_find_forward (HexDocument *doc, gint64 start, char *what, size_t len, gint64 *found);
-gboolean   hex_document_find_backward (HexDocument *doc, gint64 start, char *what, size_t len, gint64 
*found);
-gboolean    hex_document_can_undo (HexDocument *doc);
-gboolean    hex_document_can_redo (HexDocument *doc);
+gboolean       hex_document_write (HexDocument *doc);
+gboolean       hex_document_write_to_file (HexDocument *doc, GFile *file);
+gboolean       hex_document_export_html (HexDocument *doc, char *html_path,
+               char *base_name, gint64 start, gint64 end, guint cpl, guint lpp,
+               guint cpw);
+gboolean       hex_document_has_changed (HexDocument *doc);
+void           hex_document_changed (HexDocument *doc,gpointer change_data,
+               gboolean push_undo);
+void           hex_document_set_max_undo (HexDocument *doc, int max_undo);
+gboolean       hex_document_undo (HexDocument *doc);
+gboolean       hex_document_redo (HexDocument *doc);
+int                    hex_document_compare_data (HexDocument *doc, char *what,
+               gint64 pos, size_t len);
+gboolean       hex_document_find_forward (HexDocument *doc, gint64 start,
+               char *what, size_t len, gint64 *found);
+gboolean       hex_document_find_backward (HexDocument *doc, gint64 start,
+               char *what, size_t len, gint64 *found);
+gboolean       hex_document_can_undo (HexDocument *doc);
+gboolean       hex_document_can_redo (HexDocument *doc);
 gint64         hex_document_get_file_size (HexDocument *doc);
-HexChangeData *hex_document_get_undo_data (HexDocument *doc);
-
-HexBuffer * hex_document_get_buffer (HexDocument *doc);
-GFile *     hex_document_get_file (HexDocument *doc);
-gboolean    hex_document_set_file (HexDocument *doc, GFile *file);
+GFile *                hex_document_get_file (HexDocument *doc);
+gboolean       hex_document_set_file (HexDocument *doc, GFile *file);
+HexChangeData *        hex_document_get_undo_data (HexDocument *doc);
+HexBuffer *    hex_document_get_buffer (HexDocument *doc);
 
 G_END_DECLS
 


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