diff --git a/frameworks/innerkitsimpl/common/src/pixel_map.cpp b/frameworks/innerkitsimpl/common/src/pixel_map.cpp index 6bdc45806b24810fc60baea19a7218935e8a1d99..f633593d1c3ccb5ef0c08c23c3c1980be1aedee8 100644 --- a/frameworks/innerkitsimpl/common/src/pixel_map.cpp +++ b/frameworks/innerkitsimpl/common/src/pixel_map.cpp @@ -1100,7 +1100,7 @@ bool PixelMap::WriteImageData(Parcel &parcel, size_t size) const void *ptr = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) { ::close(fd); - HiLog::Error(LABEL, "WriteImageData map failed, errno:%{public}s", strerror(errno)); + HiLog::Error(LABEL, "WriteImageData map failed, errno:%{public}d", errno); return false; } HiLog::Info(LABEL, "mmap success"); @@ -1132,11 +1132,17 @@ uint8_t *PixelMap::ReadImageData(Parcel &parcel, int32_t bufferSize) int fd = -1; if (static_cast(bufferSize) <= MIN_IMAGEDATA_SIZE) { - const uint8_t *ptr = parcel.ReadUnpadBuffer(bufferSize); - if (bufferSize <= 0 || bufferSize > PIXEL_MAP_MAX_RAM_SIZE) { + if (bufferSize <= 0) { HiLog::Error(LABEL, "malloc parameter bufferSize:[%{public}d] error.", bufferSize); return nullptr; } + + const uint8_t *ptr = parcel.ReadUnpadBuffer(bufferSize); + if (ptr == nullptr) { + HiLog::Error(LABEL, "read buffer from parcel failed, read buffer addr is null"); + return nullptr; + } + base = static_cast(malloc(bufferSize)); if (base == nullptr) { HiLog::Error(LABEL, "alloc output pixel memory size:[%{public}d] error.", bufferSize); @@ -1155,22 +1161,26 @@ uint8_t *PixelMap::ReadImageData(Parcel &parcel, int32_t bufferSize) HiLog::Error(LABEL, "read fd :[%{public}d] error", fd); return nullptr; } + if (bufferSize <= 0 || bufferSize > PIXEL_MAP_MAX_RAM_SIZE) { + HiLog::Error(LABEL, "malloc parameter bufferSize:[%{public}d] error.", bufferSize); + return nullptr; + } + void *ptr = ::mmap(nullptr, bufferSize, PROT_READ, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) { // do not close fd here. fd will be closed in FileDescriptor, ::close(fd) - HiLog::Error(LABEL, "ReadImageData map failed, errno:%{public}s", strerror(errno)); - return nullptr; - } - if (bufferSize <= 0 || bufferSize > PIXEL_MAP_MAX_RAM_SIZE) { - HiLog::Error(LABEL, "malloc parameter bufferSize:[%{public}d] error.", bufferSize); + HiLog::Error(LABEL, "ReadImageData map failed, errno:%{public}d", errno); return nullptr; } + base = static_cast(malloc(bufferSize)); if (base == nullptr) { + ::munmap(ptr, bufferSize); HiLog::Error(LABEL, "alloc output pixel memory size:[%{public}d] error.", bufferSize); return nullptr; } if (memcpy_s(base, bufferSize, ptr, bufferSize) != 0) { + ::munmap(ptr, bufferSize); free(base); base = nullptr; HiLog::Error(LABEL, "memcpy pixel data size:[%{public}d] error.", bufferSize); @@ -1330,7 +1340,7 @@ PixelMap *PixelMap::Unmarshalling(Parcel &parcel) if (ptr == MAP_FAILED) { ::close(fd); delete pixelMap; - HiLog::Error(LABEL, "shared memory map failed, errno:%{public}s", strerror(errno)); + HiLog::Error(LABEL, "shared memory map failed, errno:%{public}d", errno); return nullptr; } context = new int32_t(); diff --git a/frameworks/kits/js/common/image_packer_napi.cpp b/frameworks/kits/js/common/image_packer_napi.cpp index 85ae18a74b85dcfeffe43b445e206da2bb8b71e5..5bf318eee7af421553cf22f2efe2ef0b8800190a 100644 --- a/frameworks/kits/js/common/image_packer_napi.cpp +++ b/frameworks/kits/js/common/image_packer_napi.cpp @@ -131,7 +131,6 @@ STATIC_EXEC_FUNC(Packing) context->resultBuffer = malloc(bufferSize); if (context->resultBuffer == nullptr) { HiLog::Error(LABEL, "PackingExec failed, malloc buffer failed"); - context->status = ERROR; } else { context->rImagePacker->StartPacking(static_cast(context->resultBuffer), @@ -140,11 +139,12 @@ STATIC_EXEC_FUNC(Packing) context->rImagePacker->FinalizePacking(packedSize); HiLog::Debug(LABEL, "packedSize=%{public}lld.", static_cast(packedSize)); - if (packedSize > 0 && packedSize < bufferSize) { + if (packedSize > 0 && (static_cast(packedSize) < bufferSize)) { context->packedSize = packedSize; context->status = SUCCESS; } else { context->status = ERROR; + HiLog::Error(LABEL, "Packing failed, packedSize outside size."); } } HiLog::Debug(LABEL, "PackingExec exit"); @@ -172,7 +172,7 @@ STATIC_COMPLETE_FUNC(Packing) STATIC_EXEC_FUNC(PackingFromPixelMap) { HiLog::Debug(LABEL, "PackingFromPixelMapExec enter"); - uint64_t bufferSize = 10 * 1024 * 1024; + uint64_t bufferSize = 10 * 1024 * 1024; // 10M is the maximum packedSize int64_t packedSize = 0; auto context = static_cast(data); HiLog::Debug(LABEL, "image packer get supported format"); @@ -193,11 +193,12 @@ STATIC_EXEC_FUNC(PackingFromPixelMap) context->rImagePacker->FinalizePacking(packedSize); HiLog::Debug(LABEL, "packedSize=%{public}lld.", static_cast(packedSize)); - if (packedSize > 0) { + if (packedSize > 0 && (static_cast(packedSize) < bufferSize)) { context->packedSize = packedSize; context->status = SUCCESS; } else { context->status = ERROR; + HiLog::Error(LABEL, "Packing failed, packedSize outside size."); } } HiLog::Debug(LABEL, "PackingFromPixelMapExec exit"); diff --git a/frameworks/kits/js/common/image_source_napi.cpp b/frameworks/kits/js/common/image_source_napi.cpp index 7d73e1cbe25dd3164933c7c379be9666ce2aa11a..80893243179d00979c62d4bcbe7c43abb3680fa1 100644 --- a/frameworks/kits/js/common/image_source_napi.cpp +++ b/frameworks/kits/js/common/image_source_napi.cpp @@ -86,9 +86,10 @@ static std::string GetStringArgument(napi_env env, napi_value value) if (status == napi_ok) { HiLog::Debug(LABEL, "Get Success"); strValue = buffer; + } else { + free(buffer); + buffer = nullptr; } - free(buffer); - buffer = nullptr; } return strValue; } diff --git a/interfaces/kits/js/@ohos.multimedia.image.d.ts b/interfaces/kits/js/@ohos.multimedia.image.d.ts index f94aa24b4134042c7e266d2fa3df2c01fe0f6e76..17ec79142ed591e0d7857796a99dee567bbd9605 100644 --- a/interfaces/kits/js/@ohos.multimedia.image.d.ts +++ b/interfaces/kits/js/@ohos.multimedia.image.d.ts @@ -139,20 +139,20 @@ declare namespace image { /** * Enum for image formats. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ enum ImageFormat { /** * YCBCR422 semi-planar format. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ YCBCR_422_SP = 1000, /** * JPEG encoding format. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ JPEG = 2000 @@ -200,51 +200,50 @@ declare namespace image { */ enum ScaleMode { /** - * Indicates the effect that scales an image to fill the target image area and center-crops the part outside the area. + * Indicates the effect that fits the image into the target size. * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ - CENTER_CROP = 1, // Indicates the effect that scales an image to fill the target image area and center-crops the part outside the area. + FIT_TARGET_SIZE = 0, /** - * Indicates the effect that fits the image into the target size. + * Indicates the effect that scales an image to fill the target image area and center-crops the part outside the area. * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ - FIT_TARGET_SIZE = 2, + CENTER_CROP = 1, } /** * The componet type of image. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver - * @systemapi */ enum ComponentType { /** * Luma info. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver */ YUV_Y = 1, /** * Chrominance info. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver */ YUV_U = 2, /** * Chroma info. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver */ YUV_V = 3, /** * Jpeg type. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver */ JPEG = 4, @@ -429,35 +428,34 @@ declare namespace image { /** * Describes image color components. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core - * @systemapi */ interface Component { /** * Component type. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ readonly componentType: ComponentType; /** * Row stride. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ readonly rowStride: number; /** * Pixel stride. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ readonly pixelStride: number; /** * Component buffer. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ readonly byteBuffer: ArrayBuffer; @@ -565,7 +563,7 @@ declare namespace image { /** * Creates an ImageReceiver instance. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @param width The default width in pixels of the Images that this receiver will produce. * @param height The default height in pixels of the Images that this receiver will produce. @@ -573,7 +571,6 @@ declare namespace image { * {@link ImageFormat} constants. Note that not all formats are supported, like ImageFormat.NV21. * @param capacity The maximum number of images the user will want to access simultaneously. * @return Returns the ImageReceiver instance if the operation is successful; returns null otherwise. - * @systemapi */ function createImageReceiver(width: number, height: number, format: number, capacity: number): ImageReceiver; @@ -787,7 +784,7 @@ declare namespace image { * @param options Index of the image. * @return A Promise instance used to return the property value. If the operation fails, the default value is returned. */ - getImageProperty(key:string, options?: GetImagePropertyOptions): Promise; + getImageProperty(key: string, options?: GetImagePropertyOptions): Promise; /** * Obtains the value of a property in this image. This method uses a callback to return the @@ -797,7 +794,7 @@ declare namespace image { * @param key Name of the property whose value is to be obtained. * @param callback Callback used to return the property value. If the operation fails, an error message is returned. */ - getImageProperty(key:string, callback: AsyncCallback): void; + getImageProperty(key: string, callback: AsyncCallback): void; /** * Obtains the value of a property in an image with the specified index. This method uses @@ -808,7 +805,7 @@ declare namespace image { * @param options Index of the image. * @param callback Callback used to return the property value. If the operation fails, the default value is returned. */ - getImageProperty(key:string, options: GetImagePropertyOptions, callback: AsyncCallback): void; + getImageProperty(key: string, options: GetImagePropertyOptions, callback: AsyncCallback): void; /** * Modify the value of a property in an image with the specified key. This method uses a @@ -819,7 +816,7 @@ declare namespace image { * @param value The value to be set to property. * @return A Promise instance used to return the property value. */ - modifyImageProperty(key:string, value:string): Promise; + modifyImageProperty(key: string, value: string): Promise; /** * Modify the value of a property in an image with the specified key. This method uses a callback to return the @@ -830,7 +827,7 @@ declare namespace image { * @param value The value to be set to property. * @param callback Callback to return the operation result. */ - modifyImageProperty(key:string, value:string, callback: AsyncCallback): void; + modifyImageProperty(key: string, value: string, callback: AsyncCallback): void; /** * Update the data in the incremental ImageSource. @@ -839,10 +836,10 @@ declare namespace image { * @param buf The data to be updated. * @param isFinished If is it finished. * @param value The offset of data. - * @param length The lenght fo buf. + * @param length The lenght fo buf. * @return A Promise instance used to return the property value. */ - updateData(buf:ArrayBuffer, isFinished:boolean, value:number, length: number): Promise; + updateData(buf: ArrayBuffer, isFinished: boolean, value: number, length: number): Promise; /** * Update the data in the incremental ImageSource. @@ -851,10 +848,10 @@ declare namespace image { * @param buf The data to be updated. * @param isFinished If is it finished. * @param value The offset of data. - * @param length The lenght fo buf. + * @param length The lenght fo buf. * @param callback Callback to return the operation result. */ - updateData(buf:ArrayBuffer, isFinished:boolean, value:number, length: number, callback: AsyncCallback): void; + updateData(buf: ArrayBuffer, isFinished: boolean, value: number, length: number, callback: AsyncCallback): void; /** * Releases an ImageSource instance and uses a callback to return the result. @@ -952,35 +949,34 @@ declare namespace image { /** * Provides basic image operations, including obtaining image information, and reading and writing image data. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core - * @systemapi */ interface Image { /** * Sets or gets the image area to crop, default is size. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ clipRect: Region; /** * Image size. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ readonly size: Size; /** * Image format. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core */ readonly format: number; /** * Get component buffer from image and uses a callback to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core * @param componentType The componet type of image. * @param callback Callback used to return the component buffer. @@ -989,7 +985,7 @@ declare namespace image { /** * Get component buffer from image and uses a promise to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core * @param componentType The componet type of image. * @return A Promise instance used to return the component buffer. @@ -998,7 +994,7 @@ declare namespace image { /** * Release current image to receive another and uses a callback to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core * @param callback Callback to return the operation result. */ @@ -1006,7 +1002,7 @@ declare namespace image { /** * Release current image to receive another and uses a promise to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.Core * @return A Promise instance used to return the operation result. */ @@ -1015,28 +1011,27 @@ declare namespace image { /** * Image receiver object. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver - * @systemapi */ interface ImageReceiver { /** * Image size. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver */ readonly size: Size; /** * Image capacity. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver */ readonly capacity: number; /** * Image format. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver */ readonly format: ImageFormat; @@ -1044,7 +1039,7 @@ declare namespace image { /** * get an id which indicates a surface and can be used to set to Camera or other component can receive a surface * and uses a callback to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @param callback Callback used to return the surface id. */ @@ -1053,7 +1048,7 @@ declare namespace image { /** * get an id which indicates a surface and can be used to set to Camera or other component can receive a surface * and uses a promise to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @return A Promise instance used to return the surface id. */ @@ -1061,7 +1056,7 @@ declare namespace image { /** * Get lasted image from receiver and uses a callback to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @param callback Callback used to return the latest image. */ @@ -1069,7 +1064,7 @@ declare namespace image { /** * Get lasted image from receiver and uses a promise to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @return A Promise instance used to return the latest image. */ @@ -1077,7 +1072,7 @@ declare namespace image { /** * Get next image from receiver and uses a callback to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @param callback Callback used to return the next image. */ @@ -1085,7 +1080,7 @@ declare namespace image { /** * Get next image from receiver and uses a promise to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @return A Promise instance used to return the next image. */ @@ -1093,7 +1088,7 @@ declare namespace image { /** * Subscribe callback when receiving an image - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @param type Callback used to return the next image. * @param callback Callback used to return image. @@ -1102,7 +1097,7 @@ declare namespace image { /** * Release image receiver instance and uses a callback to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @param callback Callback to return the operation result. */ @@ -1110,7 +1105,7 @@ declare namespace image { /** * Release image receiver instance and uses a promise to return the result. - * @since 8 + * @since 9 * @syscap SystemCapability.Multimedia.Image.ImageReceiver * @return A Promise instance used to return the operation result. */