From a4a9d4b703ac2baa935b715687d3aa952f03a2b7 Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Thu, 23 Mar 2023 18:09:39 +0800 Subject: [PATCH 1/2] bugfix_fs_read&write_offset Signed-off-by: 18721213663 --- .../src/mod_fs/properties/prop_n_exporter.cpp | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 606e8d144..5f900abf4 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -318,6 +318,9 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } + if (!hasOffset) { + offset = -1; + } void *buf = nullptr; int64_t len = 0; @@ -331,7 +334,6 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) return nullptr; } - ssize_t actLen; uv_buf_t buffer = uv_buf_init(static_cast(buf), len); std::unique_ptr read_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -346,8 +348,8 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) NError(errno).ThrowErr(env); return nullptr; } - actLen = ret; - return NVal::CreateInt64(env, actLen).val_; + + return NVal::CreateInt64(env, ret).val_; } static NError ReadExec(shared_ptr arg, char *buf, size_t len, int fd, size_t offset) @@ -397,6 +399,9 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } + if (!hasOffset) { + offset = -1; + } auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); if (!arg) { @@ -481,6 +486,9 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } + if (!hasOffset) { + offset = -1; + } auto arg = make_shared(move(bufGuard)); if (!arg) { @@ -548,8 +556,10 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) HILOGE("Failed to resolve buf and options"); return nullptr; } + if (!hasOffset) { + offset = -1; + } - ssize_t writeLen; uv_buf_t buffer = uv_buf_init(static_cast(buf), len); std::unique_ptr write_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -564,8 +574,8 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) NError(errno).ThrowErr(env); return nullptr; } - writeLen = ret; - return NVal::CreateInt64(env, writeLen).val_; + + return NVal::CreateInt64(env, ret).val_; } bool PropNExporter::Export() -- Gitee From 7a86103896e733421d1122a242fe2404482f5e0b Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Tue, 28 Mar 2023 09:32:35 +0800 Subject: [PATCH 2/2] bugfix_fs_read&write_init Signed-off-by: 18721213663 --- .../mod_fs/class_stream/stream_n_exporter.cpp | 12 ++-- .../mod_fs/class_stream/stream_n_exporter.h | 6 +- interfaces/kits/js/src/mod_fs/common_func.cpp | 51 ++++++++-------- interfaces/kits/js/src/mod_fs/common_func.h | 4 +- .../src/mod_fs/properties/prop_n_exporter.cpp | 58 +++++++++---------- .../src/mod_fs/properties/prop_n_exporter.h | 4 +- 6 files changed, 69 insertions(+), 66 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 5d2fc15ec..520cc4ab7 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -122,7 +122,7 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info cbInfo) HILOGE("Failed to resolve buf and options"); return nullptr; } - int ret = fseek(filp, offset, SEEK_SET); + int ret = fseek(filp, static_cast(offset), SEEK_SET); if (hasOffset && (ret < 0)) { HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); NError(errno).ThrowErr(env); @@ -130,13 +130,13 @@ napi_value StreamNExporter::WriteSync(napi_env env, napi_callback_info cbInfo) } size_t writeLen = fwrite(buf, 1, len, filp); - if (writeLen == 0 && writeLen != static_cast(len)) { + if ((writeLen == 0) && (writeLen != len)) { HILOGE("Failed to fwrite stream"); NError(EIO).ThrowErr(env); return nullptr; } - return NVal::CreateInt64(env, writeLen).val_; + return NVal::CreateInt64(env, static_cast(writeLen)).val_; } static bool HasOption(napi_env env, napi_value optionFromJsArg) @@ -181,13 +181,13 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return nullptr; } auto cbExec = [arg, buf = buf, len = len, filp, hasOffset = hasOffset, offset = offset]() -> NError { - int ret = fseek(filp, offset, SEEK_SET); + int ret = fseek(filp, static_cast(offset), SEEK_SET); if (hasOffset && (ret < 0)) { HILOGE("Failed to set the offset location of the file stream pointer, ret: %{public}d", ret); return NError(errno); } arg->actLen = fwrite(buf, 1, len, filp); - if (arg->actLen == 0 && arg->actLen != static_cast(len)) { + if ((arg->actLen == 0) && (arg->actLen != len)) { HILOGE("Failed to fwrite stream"); return NError(EIO); } @@ -198,7 +198,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) if (err) { return { env, err.GetNapiErr(env) }; } - return { NVal::CreateInt64(env, arg->actLen) }; + return { NVal::CreateInt64(env, static_cast(arg->actLen)) }; }; NVal thisVar(env, funcArg.GetThisVar()); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h index 647a1173b..dc97b5ba2 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.h @@ -44,7 +44,7 @@ public: }; struct AsyncReadArg { - size_t lenRead { 0 }; + size_t lenRead = 0; NRef refReadBuf; explicit AsyncReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {} @@ -53,8 +53,8 @@ struct AsyncReadArg { struct AsyncWrtieArg { NRef refWriteArrayBuf; - std::unique_ptr guardWriteStr; - size_t actLen { 0 }; + std::unique_ptr guardWriteStr = nullptr; + size_t actLen = 0; explicit AsyncWrtieArg(NVal refWriteArrayBuf) : refWriteArrayBuf(refWriteArrayBuf) {} explicit AsyncWrtieArg(std::unique_ptr &&guardWriteStr) : guardWriteStr(std::move(guardWriteStr)) {} diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index bcb6c355c..98ae9e395 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -67,13 +67,13 @@ void InitOpenMode(napi_env env, napi_value exports) } } -static tuple GetActualLen(napi_env env, int64_t bufLen, int64_t bufOff, NVal op) +static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufOff, NVal op) { bool succ = false; - int64_t retLen; + size_t retLen = 0; if (op.HasProp("length")) { - int64_t opLength; + int64_t opLength = 0; tie(succ, opLength) = op.GetProp("length").ToInt64(); if (!succ) { HILOGE("Invalid option.length, expect integer"); @@ -82,12 +82,12 @@ static tuple GetActualLen(napi_env env, int64_t bufLen, int64_t bu } if (opLength < 0) { retLen = bufLen - bufOff; - } else if (opLength > bufLen - bufOff) { + } else if (static_cast(opLength) > bufLen - bufOff) { HILOGE("Invalid option.length, buffer limit exceeded"); NError(EINVAL).ThrowErr(env); return { false, 0 }; } else { - retLen = opLength; + retLen = static_cast(opLength); } } else { retLen = bufLen - bufOff; @@ -196,14 +196,14 @@ tuple, unique_ptr> CommonFunc::GetCopyPathArg(n napi_value dstPath) { bool succ = false; - unique_ptr src; + unique_ptr src = nullptr; tie(succ, src, ignore) = NVal(env, srcPath).ToUTF8String(); if (!succ) { HILOGE("Failed to convert the src path to UTF-8 string"); return { false, nullptr, nullptr }; } - unique_ptr dest; + unique_ptr dest = nullptr; tie(succ, dest, ignore) = NVal(env, dstPath).ToUTF8String(); if (!succ) { HILOGE("Failed to convert the dest path to UTF-8 string"); @@ -212,11 +212,9 @@ tuple, unique_ptr> CommonFunc::GetCopyPathArg(n return make_tuple(true, move(src), move(dest)); } -static tuple, int64_t> DecodeString(napi_env env, NVal jsStr, NVal encoding) +static tuple, size_t> DecodeString(napi_env env, NVal jsStr, NVal encoding) { - unique_ptr buf; if (!jsStr.TypeIs(napi_string)) { - HILOGE("Failed to recognize the type to a string"); return { false, nullptr, 0 }; } @@ -225,7 +223,7 @@ static tuple, int64_t> DecodeString(napi_env env, NVal return jsStr.ToUTF8String(); } - unique_ptr encodingBuf; + unique_ptr encodingBuf = nullptr; tie(succ, encodingBuf, ignore) = encoding.ToUTF8String(); if (!succ) { return { false, nullptr, 0 }; @@ -241,20 +239,20 @@ static tuple, int64_t> DecodeString(napi_env env, NVal } } -tuple CommonFunc::GetReadArg(napi_env env, +tuple CommonFunc::GetReadArg(napi_env env, napi_value readBuf, napi_value option) { - int64_t retLen; - int64_t position; + size_t retLen = 0; + int64_t position = 0; bool succ = false; bool posAssigned = false; NVal txt(env, readBuf); void *buf = nullptr; - int64_t bufLen; + size_t bufLen = 0; tie(succ, buf, bufLen) = txt.ToArraybuffer(); - if (!succ) { - HILOGE("Invalid read buffer, expect arraybuffer"); + if (!succ || bufLen > UINT_MAX) { + HILOGE("Invalid arraybuffer"); NError(EINVAL).ThrowErr(env); return { false, nullptr, 0, posAssigned, position }; } @@ -279,18 +277,17 @@ tuple CommonFunc::GetReadArg(napi_env env, return { true, buf, retLen, posAssigned, position }; } -tuple, void *, int64_t, bool, int64_t> CommonFunc::GetWriteArg(napi_env env, +tuple, void *, size_t, bool, int64_t> CommonFunc::GetWriteArg(napi_env env, napi_value argWBuf, napi_value argOption) { - int64_t retLen; - int64_t retPos; - int64_t bufLen; + int64_t retPos = 0; + size_t bufLen = 0; bool hasPos = false; bool succ = false; void *buf = nullptr; NVal op(env, argOption); NVal jsBuffer(env, argWBuf); - unique_ptr bufferGuard; + unique_ptr bufferGuard = nullptr; tie(succ, bufferGuard, bufLen) = DecodeString(env, jsBuffer, op.GetProp("encoding")); if (!succ) { tie(succ, buf, bufLen) = NVal(env, argWBuf).ToArraybuffer(); @@ -302,6 +299,12 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW } else { buf = bufferGuard.get(); } + if (bufLen > UINT_MAX) { + HILOGE("The Size of buffer is too large"); + NError(EINVAL).ThrowErr(env); + return { false, nullptr, nullptr, 0, hasPos, retPos} ; + } + size_t retLen = 0; tie(succ, retLen) = GetActualLen(env, bufLen, 0, op); if (!succ) { HILOGE("Failed to get actual length"); @@ -309,8 +312,8 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW } if (op.HasProp("offset")) { - int32_t position = 0; - tie(succ, position) = op.GetProp("offset").ToInt32(); + int64_t position = 0; + tie(succ, position) = op.GetProp("offset").ToInt64(); if (!succ || position < 0) { HILOGE("option.offset shall be positive number"); NError(EINVAL).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 0736d7b45..e18d4dbfa 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -48,10 +48,10 @@ struct CommonFunc { static unsigned int ConvertJsFlags(unsigned int &flags); static LibN::NVal InstantiateStat(napi_env env, struct stat &buf); static LibN::NVal InstantiateStream(napi_env env, std::unique_ptr fp); - static std::tuple GetReadArg(napi_env env, + static std::tuple GetReadArg(napi_env env, napi_value readBuf, napi_value option); - static std::tuple, void *, int64_t, bool, int64_t> GetWriteArg(napi_env env, + static std::tuple, void *, size_t, bool, int64_t> GetWriteArg(napi_env env, napi_value argWBuf, napi_value argOption); static std::tuple, std::unique_ptr> GetCopyPathArg(napi_env env, diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 5f900abf4..3a234482f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -313,17 +313,14 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) bool succ = false; int fd = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - if (!hasOffset) { - offset = -1; - } void *buf = nullptr; - int64_t len = 0; + size_t len = 0; bool hasOffset = false; int64_t offset = 0; tie(succ, buf, len, hasOffset, offset) = @@ -333,8 +330,11 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } + if (!hasOffset) { + offset = -1; + } - uv_buf_t buffer = uv_buf_init(static_cast(buf), len); + uv_buf_t buffer = uv_buf_init(static_cast(buf), static_cast(len)); std::unique_ptr read_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!read_req) { @@ -349,10 +349,10 @@ napi_value PropNExporter::ReadSync(napi_env env, napi_callback_info info) return nullptr; } - return NVal::CreateInt64(env, ret).val_; + return NVal::CreateInt64(env, static_cast(ret)).val_; } -static NError ReadExec(shared_ptr arg, char *buf, size_t len, int fd, size_t offset) +static NError ReadExec(shared_ptr arg, char *buf, size_t len, int32_t fd, int64_t offset) { uv_buf_t buffer = uv_buf_init(buf, len); std::unique_ptr read_req = { @@ -361,7 +361,7 @@ static NError ReadExec(shared_ptr arg, char *buf, size_t len, in HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_read(nullptr, read_req.get(), fd, &buffer, 1, static_cast(offset), nullptr); + int ret = uv_fs_read(nullptr, read_req.get(), fd, &buffer, 1, offset, nullptr); if (ret < 0) { HILOGE("Failed to read file for %{public}d", ret); return NError(errno); @@ -381,12 +381,12 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) bool succ = false; void *buf = nullptr; - int64_t len = 0; - int fd = 0; + size_t len = 0; + int32_t fd = 0; bool hasOffset = false; int64_t offset = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -418,7 +418,7 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } - return { NVal::CreateInt64(env, arg->lenRead) }; + return { NVal::CreateInt64(env, static_cast(arg->lenRead)) }; }; NVal thisVar(env, funcArg.GetThisVar()); @@ -438,16 +438,16 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) } } -static NError WriteExec(shared_ptr arg, char *buf, size_t len, int fd, size_t offset) +static NError WriteExec(shared_ptr arg, char *buf, size_t len, int32_t fd, int64_t offset) { - uv_buf_t buffer = uv_buf_init(buf, len); + uv_buf_t buffer = uv_buf_init(buf, static_cast(len)); std::unique_ptr write_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!write_req) { HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, static_cast(offset), nullptr); + int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, offset, nullptr); if (ret < 0) { HILOGE("Failed to write file for %{public}d", ret); return NError(errno); @@ -466,18 +466,18 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) } bool succ = false; - int fd; + int32_t fd = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; } - unique_ptr bufGuard; + unique_ptr bufGuard = nullptr; void *buf = nullptr; size_t len = 0; - size_t offset = 0; + int64_t offset = 0; bool hasOffset = false; tie(succ, bufGuard, buf, len, hasOffset, offset) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); @@ -505,7 +505,7 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } else { - return { NVal::CreateInt64(env, arg->actLen) }; + return { NVal::CreateInt64(env, static_cast(arg->actLen)) }; } }; @@ -537,9 +537,9 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) } bool succ = false; - int fd; + int32_t fd = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); - if (!succ) { + if (!succ || fd < 0) { HILOGE("Invalid fd from JS first argument"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -547,8 +547,8 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) void *buf = nullptr; size_t len = 0; - size_t offset = 0; - unique_ptr bufGuard; + int64_t offset = 0; + unique_ptr bufGuard = nullptr; bool hasOffset = false; tie(succ, bufGuard, buf, len, hasOffset, offset) = CommonFunc::GetWriteArg(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); @@ -560,7 +560,7 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) offset = -1; } - uv_buf_t buffer = uv_buf_init(static_cast(buf), len); + uv_buf_t buffer = uv_buf_init(static_cast(buf), static_cast(len)); std::unique_ptr write_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; if (!write_req) { @@ -568,14 +568,14 @@ napi_value PropNExporter::WriteSync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, static_cast(offset), nullptr); + int ret = uv_fs_write(nullptr, write_req.get(), fd, &buffer, 1, offset, nullptr); if (ret < 0) { HILOGE("Failed to write file for %{public}d", ret); NError(errno).ThrowErr(env); return nullptr; } - return NVal::CreateInt64(env, ret).val_; + return NVal::CreateInt64(env, static_cast(ret)).val_; } bool PropNExporter::Export() @@ -638,4 +638,4 @@ PropNExporter::PropNExporter(napi_env env, napi_value exports) : NExporter(env, PropNExporter::~PropNExporter() {} } // namespace ModuleFileIO } // namespace FileManagement -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h index b6ecca506..6db096808 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.h @@ -26,7 +26,7 @@ using namespace OHOS::FileManagement::LibN; struct AsyncIOWrtieArg { NRef refWriteArrayBuf_; std::unique_ptr guardWriteStr_; - ssize_t actLen = 0; + int actLen = 0; explicit AsyncIOWrtieArg(NVal refWriteArrayBuf) : refWriteArrayBuf_(refWriteArrayBuf) {} explicit AsyncIOWrtieArg(std::unique_ptr &&guardWriteStr) : guardWriteStr_(move(guardWriteStr)) {} @@ -38,7 +38,7 @@ struct AsyncAccessArg { }; struct AsyncIOReadArg { - ssize_t lenRead { 0 }; + int lenRead = 0; NRef refReadBuf; explicit AsyncIOReadArg(NVal jsReadBuf) : refReadBuf(jsReadBuf) {} -- Gitee