Fix io.buffer (2 asan crashes, 1 missbehaviour) (#11048)

This commit is contained in:
radare 2018-08-12 20:26:28 +02:00 committed by GitHub
parent 1464b19d36
commit 237d74524b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 21 deletions

View File

@ -429,6 +429,7 @@ R_API int r_io_cache_invalidate(RIO *io, ut64 from, ut64 to);
R_API bool r_io_cache_at(RIO *io, ut64 addr);
R_API void r_io_cache_commit(RIO *io, ut64 from, ut64 to);
R_API void r_io_cache_init(RIO *io);
R_API void r_io_cache_fini (RIO *io);
R_API int r_io_cache_list(RIO *io, int rad);
R_API void r_io_cache_reset(RIO *io, int set);
R_API bool r_io_cache_write(RIO *io, ut64 addr, const ut8 *buf, int len);

View File

@ -34,6 +34,15 @@ R_API bool r_io_cache_at(RIO *io, ut64 addr) {
R_API void r_io_cache_init(RIO *io) {
io->cache = r_list_newf ((RListFree)cache_item_free);
io->buffer = r_cache_new ();
io->cached = 0;
}
R_API void r_io_cache_fini (RIO *io) {
r_list_free (io->cache);
r_cache_free (io->buffer);
io->cache = NULL;
io->buffer = NULL;
io->cached = 0;
}

View File

@ -219,7 +219,9 @@ R_API RIO* r_io_init(RIO* io) {
R_API RBuffer *r_io_read_buf(RIO *io, ut64 addr, int len) {
RBuffer *b = R_NEW0 (RBuffer);
if (!b) return NULL;
if (!b) {
return NULL;
}
b->buf = malloc (len);
if (!b->buf) {
free (b);
@ -248,7 +250,7 @@ R_API RIODesc *r_io_open_buffer(RIO *io, RBuffer *b, int flags, int mode) {
char *uri = r_str_newf ("malloc://%d", bufSize);
RIODesc *desc = r_io_open_nomap (io, uri, flags, mode);
if (desc) {
r_io_desc_write (desc, r_buf_get_at(b, 0, NULL), bufSize);
r_io_desc_write (desc, r_buf_get_at (b, 0, NULL), bufSize);
}
return desc;
}
@ -369,11 +371,10 @@ R_API int r_io_close_all(RIO* io) { // what about undo?
r_io_map_fini (io);
r_io_section_fini (io);
ls_free (io->plugins);
r_list_free (io->cache);
r_io_desc_init (io);
r_io_map_init (io);
r_io_section_init (io);
r_io_cache_init (io);
r_io_cache_fini (io);
r_io_plugin_init (io);
return true;
}
@ -463,15 +464,16 @@ R_API bool r_io_read_at(RIO *io, ut64 addr, ut8 *buf, int len) {
return false;
}
if (io->buffer_enabled) {
return !!r_io_buffer_read(io, addr, buf, len);
int res = r_io_buffer_read (io, addr, buf, len);
return res > 0? true: false;
}
if (io->va) {
ret = r_io_vread_at_mapped(io, addr, buf, len);
ret = r_io_vread_at_mapped (io, addr, buf, len);
} else {
ret = r_io_pread_at(io, addr, buf, len) > 0;
ret = r_io_pread_at (io, addr, buf, len) > 0;
}
if (io->cached & R_IO_READ) {
(void)r_io_cache_read(io, addr, buf, len);
(void)r_io_cache_read (io, addr, buf, len);
}
return ret;
}
@ -589,10 +591,7 @@ R_API bool r_io_write_at(RIO* io, ut64 addr, const ut8* buf, int len) {
}
R_API bool r_io_read(RIO* io, ut8* buf, int len) {
if (!io) {
return false;
}
if (r_io_read_at (io, io->off, buf, len)) {
if (io && r_io_read_at (io, io->off, buf, len)) {
io->off += len;
return true;
}
@ -600,10 +599,7 @@ R_API bool r_io_read(RIO* io, ut8* buf, int len) {
}
R_API bool r_io_write(RIO* io, ut8* buf, int len) {
if (!io || !buf || len < 1) {
return false;
}
if (r_io_write_at (io, io->off, buf, len)) {
if (io && buf && len > 0 && r_io_write_at (io, io->off, buf, len)) {
io->off += len;
return true;
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2013-2017 - pancake */
/* radare - LGPL - Copyright 2013-2018 - pancake */
#include "r_io.h"
@ -34,10 +34,9 @@ R_API const ut8* r_io_buffer_get (RIO *io, ut64 addr, int *len) {
}
R_API int r_io_buffer_read (RIO *io, ut64 addr, ut8* buf, int len) {
const ut8 *ret;
int next, l = 0;
// align addr if out of buffer if its mapped on io //
ret = r_cache_get (io->buffer, addr, &l);
const ut8 *ret = r_cache_get (io->buffer, addr, &l);
if (!ret) {
if (l < 1) {
return 0; // no next block in buffer cache
@ -57,7 +56,7 @@ R_API int r_io_buffer_read (RIO *io, ut64 addr, ut8* buf, int len) {
l = len;
}
memset (buf, 0xff, next);
memcpy (buf + next, ret, len - next);
memcpy (buf + next, ret, R_MIN (l, len - next));
return len;
}
if (l > len) {

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2013-2017 - pancake */
/* radare - LGPL - Copyright 2013-2018 - pancake */
// XXX: should use the same code as libr/io/cache.c
// one malloc per write
@ -44,6 +44,9 @@ R_API const ut8 *r_cache_get(RCache *c, ut64 addr, int *len) {
}
R_API int r_cache_set(RCache *c, ut64 addr, const ut8 *buf, int len) {
if (!c) {
return 0;
}
if (!c->buf) {
c->buf = malloc (len);
if (!c->buf) {