From 292444e3f5c2c1df45a949cca0a0be430a5ba57c Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Wed, 4 Feb 2015 14:46:22 +0100 Subject: RNA image.pack(): fix possible memleak, add possibility to pass raw bytes data. Note passing data assumes user knows what he is doing, else segfault is guaranted (there is no good ways to pass raw bytes data to RNA func currently, and using int array is way too heavy in this case). And image->packedfile was never freed... --- source/blender/makesrna/intern/rna_image_api.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'source/blender') diff --git a/source/blender/makesrna/intern/rna_image_api.c b/source/blender/makesrna/intern/rna_image_api.c index 90f90ea8632..63bd50e7ada 100644 --- a/source/blender/makesrna/intern/rna_image_api.c +++ b/source/blender/makesrna/intern/rna_image_api.c @@ -144,7 +144,7 @@ static void rna_Image_save(Image *image, bContext *C, ReportList *reports) WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, image); } -static void rna_Image_pack(Image *image, bContext *C, ReportList *reports, int as_png) +static void rna_Image_pack(Image *image, bContext *C, ReportList *reports, int as_png, const char *data, int data_len) { ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL); @@ -152,9 +152,18 @@ static void rna_Image_pack(Image *image, bContext *C, ReportList *reports, int a BKE_report(reports, RPT_ERROR, "Cannot pack edited image from disk, only as internal PNG"); } else { + if (image->packedfile) { + freePackedFile(image->packedfile); + image->packedfile = NULL; + } if (as_png) { BKE_image_memorypack(image); } + else if (data) { + char *data_dup = MEM_mallocN(sizeof(*data_dup) * (size_t)data_len, __func__); + memcpy(data_dup, data, (size_t)data_len); + image->packedfile = newPackedFileMemory(data_dup, data_len); + } else { image->packedfile = newPackedFile(reports, image->name, ID_BLEND_PATH(G.main, &image->id)); } @@ -307,6 +316,10 @@ void RNA_api_image(StructRNA *srna) RNA_def_function_ui_description(func, "Pack an image as embedded data into the .blend file"); RNA_def_function_flag(func, FUNC_USE_CONTEXT | FUNC_USE_REPORTS); RNA_def_boolean(func, "as_png", 0, "as_png", "Pack the image as PNG (needed for generated/dirty images)"); + parm = RNA_def_property(func, "data", PROP_STRING, PROP_BYTESTRING); + RNA_def_property_ui_text(parm, "data", "Raw data (bytes, exact content of the embedded file)"); + RNA_def_int(func, "data_len", 0, 0, INT_MAX, + "data_len", "length of given data (mandatory if data is provided)", 0, INT_MAX); func = RNA_def_function(srna, "unpack", "rna_Image_unpack"); RNA_def_function_ui_description(func, "Save an image packed in the .blend file to disk"); -- cgit v1.2.3