1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
Description: Fix segfault on save failure
dhex crashes when file IO returns any error, lack of error handling.
Author: Fabio Augusto De Muzio Tobich <ftobich@debian.org>
Bug-Debian: https://bugs.debian.org/889956
Forwarded: no
Last-Update: 2025-12-19
Index: dhex/buffers.c
===================================================================
--- dhex.orig/buffers.c
+++ dhex/buffers.c
@@ -62,8 +62,15 @@ tUInt32 readbuf(tBuffer* hBuf,tInt64 pos
hBuf->fresh=0;
if (hBuf->filesize>hBuf->bufferpos)
{
- setfilepos(hBuf->file,hBuf->bufferpos);
+ if (setfilepos(hBuf->file,hBuf->bufferpos)!=RETOK)
+ {
+ return RETNOK;
+ }
bytesread=fread(hBuf->data,sizeof(tUInt8),BUFFERSIZE,hBuf->file);
+ if (bytesread!=BUFFERSIZE && ferror(hBuf->file))
+ {
+ return RETNOK;
+ }
if (bytesread!=BUFFERSIZE)
{
memset(&hBuf->data[bytesread],0,sizeof(tUInt8)*(BUFFERSIZE-bytesread)); // fill the rest with 0
@@ -78,12 +85,12 @@ tUInt32 readbuf(tBuffer* hBuf,tInt64 pos
tInt32 getbufferidx(tBuffer* hBuf,tInt64 pos)
{
- tInt32 retval=OK;
+ tInt32 retval=RETOK;
if ((pos<(hBuf->bufferpos+BUFFERMARGIN)) || (pos>(hBuf->bufferpos+BUFFERSIZE-BUFFERMARGIN)))
{
retval=readbuf(hBuf,pos);
}
- if (retval==OK)
+ if (retval==RETOK)
{
retval=pos-(hBuf->bufferpos);
}
@@ -92,18 +99,52 @@ tInt32 getbufferidx(tBuffer* hBuf,tInt64
tInt8 savechanges(tBuffer* hBuf)
{
int i;
+ size_t bytes_written;
+
+ // Close the file temporarily
fclose(hBuf->file);
+
if (hBuf->changesnum)
{
- hBuf->file=fopen(hBuf->filename,"r+b");
- if (hBuf->file==NULL) return RETNOK;
- for (i=0;i<hBuf->changesnum;i++)
+ // Try to open for writing
+ hBuf->file = fopen(hBuf->filename, "r+b");
+ if (hBuf->file == NULL) {
+ // Failed to open for writing, try to reopen for reading
+ hBuf->file = fopen(hBuf->filename, "rb");
+ return RETNOK;
+ }
+
+ for (i = 0; i < hBuf->changesnum; i++)
{
- setfilepos(hBuf->file,hBuf->changes[i].pos);
- fwrite(&(hBuf->changes[i].after),sizeof(tUInt8),1,hBuf->file);
+ if (setfilepos(hBuf->file, hBuf->changes[i].pos) == RETNOK)
+ {
+ fclose(hBuf->file);
+ // Try to reopen for reading
+ hBuf->file = fopen(hBuf->filename, "rb");
+ return RETNOK;
+ }
+ bytes_written = fwrite(&(hBuf->changes[i].after), sizeof(tUInt8), 1, hBuf->file);
+ if (bytes_written != 1)
+ {
+ fclose(hBuf->file);
+ // Try to reopen for reading
+ hBuf->file = fopen(hBuf->filename, "rb");
+ return RETNOK;
+ }
}
- fclose(hBuf->file);
+ if (fclose(hBuf->file) == EOF)
+ {
+ // Try to reopen for reading
+ hBuf->file = fopen(hBuf->filename, "rb");
+ return RETNOK;
+ }
}
+
+ // Reopen for reading
+ hBuf->file = fopen(hBuf->filename, "rb");
+ if (hBuf->file == NULL) {
+ return RETNOK;
+ }
+
return RETOK;
-}
-
+}
\ No newline at end of file
Index: dhex/machine_type.c
===================================================================
--- dhex.orig/machine_type.c
+++ dhex/machine_type.c
@@ -16,8 +16,12 @@ tUInt64 getfilesize(tFptr f)
return x;
//return (tUInt64)ftell(f);
}
-void setfilepos(tFptr f,tUInt64 pos)
+tInt8 setfilepos(tFptr f,tUInt64 pos)
{
- fseek(f,pos,SEEK_SET);
+ if (fseek(f,pos,SEEK_SET)!=0)
+ {
+ return RETNOK;
+ }
+ return RETOK;
}
Index: dhex/machine_type.h
===================================================================
--- dhex.orig/machine_type.h
+++ dhex/machine_type.h
@@ -20,7 +20,7 @@ typedef FILE* tFptr;
// number 2: file operations
tUInt64 getfilepos(tFptr f);
tUInt64 getfilesize(tFptr f);
-void setfilepos(tFptr f,tUInt64 pos);
+tInt8 setfilepos(tFptr f,tUInt64 pos);
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
Index: dhex/ui.c
===================================================================
--- dhex.orig/ui.c
+++ dhex/ui.c
@@ -131,9 +131,13 @@ tInt8 savedialog(tOutput* output,tBuffer
{
if (savechanges(hBuf)!=RETOK)
{
- drawcenterframe(output,3,22,"ERROR");
+ // Redraw the same frame with error message
+ drawcenterframe(output,3,26,"Save Error");
setcolor(output,COLOR_TEXT);
- mvwprintw(output->win,offsy+1,offsx+1,"Could not save\n");
+ mvwprintw(output->win,offsy+1,offsx+1,"File is read-only");
+ mvwprintw(output->win,offsy+2,offsx+1,"Cannot save changes");
+ wrefresh(output->win);
+ getkey((tKeyTab*)output->pKeyTab,0);
return RETNOK;
}
}
|