1- /* radare - LGPL - Copyright 2013-2024 - pancake */
1+ /* radare - LGPL - Copyright 2013-2025 - pancake */
22
33#include <r_userconf.h>
44#include <r_io.h>
55#include <r_lib.h>
66
77typedef struct r_io_mmo_t {
8- char * filename ;
8+ char * filename ;
99 int mode ;
1010 int flags ;
1111 int fd ;
1212 int opened ;
1313 ut8 modified ;
1414 RBuffer * buf ;
15- RIO * io_backref ;
15+ RIO * io_backref ;
1616} RIOMMapFileObj ;
1717
1818static ut64 r_io_mmap_seek (RIO * io , RIOMMapFileObj * mmo , ut64 offset , int whence ) {
@@ -21,16 +21,16 @@ static ut64 r_io_mmap_seek(RIO *io, RIOMMapFileObj *mmo, ut64 offset, int whence
2121 case R_IO_SEEK_SET :
2222 seek_val = (r_buf_size (mmo -> buf ) < offset )? r_buf_size (mmo -> buf ): offset ;
2323 r_buf_seek (mmo -> buf , io -> off = seek_val , R_BUF_SET );
24- return seek_val ;
24+ break ;
2525 case R_IO_SEEK_CUR :
2626 seek_val = (r_buf_size (mmo -> buf ) < (offset + r_buf_tell (mmo -> buf )))? r_buf_size (mmo -> buf ):
2727 offset + r_buf_tell (mmo -> buf );
2828 r_buf_seek (mmo -> buf , io -> off = seek_val , R_BUF_SET );
29- return seek_val ;
29+ break ;
3030 case R_IO_SEEK_END :
3131 seek_val = r_buf_size (mmo -> buf );
3232 r_buf_seek (mmo -> buf , io -> off = seek_val , R_BUF_SET );
33- return seek_val ;
33+ break ;
3434 }
3535 return seek_val ;
3636}
@@ -50,18 +50,16 @@ static bool r_io_mmap_refresh_buf(RIOMMapFileObj *mmo) {
5050}
5151
5252static void r_io_mmap_free (RIOMMapFileObj * mmo ) {
53- free (mmo -> filename );
54- r_buf_free (mmo -> buf );
55- memset (mmo , 0 , sizeof (RIOMMapFileObj ));
56- free (mmo );
53+ if (mmo ) {
54+ free (mmo -> filename );
55+ r_buf_free (mmo -> buf );
56+ free (mmo );
57+ }
5758}
5859
5960RIOMMapFileObj * r_io_mmap_create_new_file (RIO * io , const char * filename , int mode , int flags ) {
6061 R_RETURN_VAL_IF_FAIL (io && filename , NULL );
6162 RIOMMapFileObj * mmo = R_NEW0 (RIOMMapFileObj );
62- if (!mmo ) {
63- return NULL ;
64- }
6563 mmo -> filename = strdup (filename );
6664 mmo -> fd = r_num_rand (0xFFFF ); // XXX: Use r_io_fd api
6765 mmo -> mode = mode ;
@@ -95,23 +93,19 @@ static int r_io_mmap_read(RIO *io, RIODesc *fd, ut8 *buf, int count) {
9593}
9694
9795static int r_io_mmap_write (RIO * io , RIODesc * fd , const ut8 * buf , int count ) {
98- RIOMMapFileObj * mmo ;
99- int len = count ;
100- ut64 addr ;
101-
10296 if (!io || !fd || !fd -> data || !buf ) {
10397 return -1 ;
10498 }
105- mmo = fd -> data ;
106- addr = io -> off ;
99+ RIOMMapFileObj * mmo = fd -> data ;
100+ ut64 addr = io -> off ;
107101 if (!(mmo -> flags & R_PERM_W )) {
108102 return -1 ;
109103 }
110104 if ((count + addr > r_buf_size (mmo -> buf )) || r_buf_size (mmo -> buf ) == 0 ) {
111105 ut64 sz = count + addr ;
112106 r_file_truncate (mmo -> filename , sz );
113107 }
114- len = r_file_mmap_write (mmo -> filename , io -> off , buf , len );
108+ int len = r_file_mmap_write (mmo -> filename , io -> off , buf , count );
115109 if (!r_io_mmap_refresh_buf (mmo ) ) {
116110 R_LOG_ERROR ("failed to refresh the mmap backed buffer" );
117111 // XXX - not sure what needs to be done here (error handling).
0 commit comments