diff --git a/libr/anal/op.c b/libr/anal/op.c index 5b1fab4ca7214..4b142f183a366 100644 --- a/libr/anal/op.c +++ b/libr/anal/op.c @@ -206,7 +206,7 @@ R_API RAnalOp *r_anal_op_copy(RAnalOp *op) { nop->src[2] = r_anal_value_copy (op->src[2]); nop->dst = r_anal_value_copy (op->dst); r_strbuf_init (&nop->esil); - r_strbuf_set (&nop->esil, r_strbuf_get (&op->esil)); + r_strbuf_copy (&nop->esil, &op->esil); return nop; } diff --git a/libr/include/r_util/r_strbuf.h b/libr/include/r_util/r_strbuf.h index f9d60d25c5116..ac2bf7d91267a 100644 --- a/libr/include/r_util/r_strbuf.h +++ b/libr/include/r_util/r_strbuf.h @@ -30,6 +30,7 @@ R_API int r_strbuf_length(RStrBuf *sb); R_API void r_strbuf_free(RStrBuf *sb); R_API void r_strbuf_fini(RStrBuf *sb); R_API void r_strbuf_init(RStrBuf *sb); +R_API bool r_strbuf_copy(RStrBuf *dst, RStrBuf *src); R_API bool r_strbuf_equals(RStrBuf *sa, RStrBuf *sb); R_API bool r_strbuf_reserve(RStrBuf *sb, int len); diff --git a/libr/util/strbuf.c b/libr/util/strbuf.c index 497a8a3bb82c8..dd92ebdca1af9 100644 --- a/libr/util/strbuf.c +++ b/libr/util/strbuf.c @@ -30,6 +30,25 @@ R_API void r_strbuf_init(RStrBuf *sb) { memset (sb, 0, sizeof (RStrBuf)); } +R_API bool r_strbuf_copy(RStrBuf *dst, RStrBuf *src) { + r_return_val_if_fail (dst && src, false); + if (src->ptr) { + char *p = malloc (src->ptrlen); + if (!p) { + return false; + } + memcpy (p, src->ptr, src->ptrlen); + free (dst->ptr); + dst->ptr = p; + dst->ptrlen = src->ptrlen; + } else { + R_FREE (dst->ptr); + memcpy (dst->buf, src->buf, sizeof (dst->buf)); + } + dst->len = src->len; + return true; +} + R_API bool r_strbuf_reserve(RStrBuf *sb, int len) { r_return_val_if_fail (sb && len > 0, false);