From 60cdcef05d85455c13b31a94e4b36936b95d5b70 Mon Sep 17 00:00:00 2001 From: yaomanhai Date: Thu, 9 Sep 2021 08:46:02 +0000 Subject: [PATCH] Codex issue fix Signed-off-by: yaomanhai --- .../native/include/seq_packet_socket_client.h | 2 +- services/hilogd/include/log_compress.h | 1 + .../hilogd/include/log_persister_rotator.h | 2 +- services/hilogd/log_compress.cpp | 33 +++++++++++-------- services/hilogd/log_persister.cpp | 1 + services/hilogd/log_persister_rotator.cpp | 23 +++++++++---- services/hilogd/log_querier.cpp | 10 +++--- 7 files changed, 44 insertions(+), 28 deletions(-) diff --git a/frameworks/native/include/seq_packet_socket_client.h b/frameworks/native/include/seq_packet_socket_client.h index 90fdf01..4549131 100644 --- a/frameworks/native/include/seq_packet_socket_client.h +++ b/frameworks/native/include/seq_packet_socket_client.h @@ -30,7 +30,7 @@ class SeqPacketSocketClient : public SocketClient { public: SeqPacketSocketClient(std::string serverPath, int socketOption) : SocketClient(serverPath, SOCK_SEQPACKET) { - int socketType = SOCK_SEQPACKET | (socketOption & allowOption); + int socketType = SOCK_SEQPACKET | ((uint32_t)socketOption & (uint32_t)allowOption); SetType(socketType); } ~SeqPacketSocketClient() = default; diff --git a/services/hilogd/include/log_compress.h b/services/hilogd/include/log_compress.h index 0a32f76..8dad94e 100644 --- a/services/hilogd/include/log_compress.h +++ b/services/hilogd/include/log_compress.h @@ -38,6 +38,7 @@ public: LogCompress(); virtual ~LogCompress() = default; virtual int Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &compressBuffer) = 0; + void DeleteZData(); unsigned char *zdata = nullptr; char buffIn[CHUNK] = {0}; char buffOut[CHUNK] = {0}; diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index eda4ec9..695ee63 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -30,7 +30,7 @@ typedef struct { } PersistRecoveryInfo; const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; -uLong GetInfoCRC32(const PersistRecoveryInfo &info); +uint64_t GetInfoHash(const PersistRecoveryInfo &info); class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index 6a997af..c9402ac 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -26,6 +26,12 @@ LogCompress::LogCompress() { } +void LogCompress::DeleteZData() +{ + delete zdata; + zdata = nullptr; +} + int NoneCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &compressBuffer) { if (memcpy_s(compressBuffer->content, compressBuffer->offset, buffer->content, buffer->offset) != 0) { @@ -51,8 +57,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cStream.zfree = Z_NULL; cStream.opaque = Z_NULL; if (deflateInit2(&cStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) { - delete zdata; - zdata = nullptr; + DeleteZData();; return -1; } do { @@ -60,7 +65,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, read - src_pos) != 0) { - delete zdata; + DeleteZData(); return -1; } cStream.avail_in = read - src_pos; @@ -68,7 +73,7 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com } else { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, toRead) != 0) { - delete zdata; + DeleteZData(); return -1; }; src_pos += toRead; @@ -82,12 +87,12 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cStream.avail_out = CHUNK; cStream.next_out = (Bytef *)buffOut; if (deflate(&cStream, flush) == Z_STREAM_ERROR) { - delete zdata; + DeleteZData(); return -1; } unsigned have = CHUNK - cStream.avail_out; if (memmove_s(zdata + dst_pos, CHUNK, buffOut, have) != 0) { - delete zdata; + DeleteZData(); return -1; } dst_pos += have; @@ -97,11 +102,11 @@ int ZlibCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com (void)deflateEnd(&cStream); if (memcpy_s(compressBuffer->content + compressBuffer->offset, MAX_PERSISTER_BUFFER_SIZE - compressBuffer->offset, zdata, dst_pos) != 0) { - delete zdata; + DeleteZData(); return -1; } compressBuffer->offset += dst_pos; - delete zdata; + DeleteZData(); return 0; } @@ -119,7 +124,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com cctx = ZSTD_createCCtx(); if (cctx == nullptr) { cout << "ZSTD_createCCtx() failed!" << endl; - delete zdata; + DeleteZData();; return -1; } ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, compressionlevel); @@ -133,7 +138,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com if (flag) { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, read - src_pos) != 0) { - delete zdata; + DeleteZData(); return -1; } input = {buffIn, read - src_pos, 0}; @@ -141,7 +146,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com } else { memset_s(buffIn, CHUNK, 0, CHUNK); if (memmove_s(buffIn, CHUNK, buffer->content + src_pos, toRead) != 0) { - delete zdata; + DeleteZData(); return -1; } input = {buffIn, toRead, 0}; @@ -153,7 +158,7 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com ZSTD_outBuffer output = {buffOut, CHUNK, 0}; size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode); if (memmove_s(zdata + dst_pos, zdlen, (Bytef *)buffOut, output.pos) != 0) { - delete zdata; + DeleteZData(); return -1; } dst_pos += output.pos; @@ -163,11 +168,11 @@ int ZstdCompress::Compress(LogPersisterBuffer* &buffer, LogPersisterBuffer* &com ZSTD_freeCCtx(cctx); if (memcpy_s(compressBuffer->content + compressBuffer->offset, MAX_PERSISTER_BUFFER_SIZE - compressBuffer->offset, zdata, dst_pos) != 0) { - delete zdata; + DeleteZData(); return -1; } compressBuffer->offset += dst_pos; - delete zdata; + DeleteZData(); #endif // #ifdef USING_ZSTD_COMPRESS return 0; } diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 4831bfa..93929f6 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -59,6 +59,7 @@ LogPersister::LogPersister(uint32_t id, string path, uint32_t fileSize, uint16_t hilogBuffer = &_buffer; compressor = nullptr; buffer = nullptr; + compressBuffer = nullptr; plainLogSize = 0; } diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 407172f..9207c75 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -25,11 +25,19 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -uLong GetInfoCRC32(const PersistRecoveryInfo &info) -{ - uLong crc = crc32(0L, Z_NULL, 0); - crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); - return crc; +constexpr uint64_t PRIME = 0x100000001B3ull; +constexpr uint64_t BASIS = 0xCBF29CE484222325ull; +uint64_t GetInfoHash(const PersistRecoveryInfo &info) +{ + uint64_t ret {BASIS}; + const char *p = (char *)&info; + unsigned long i = 0; + while (i < sizeof(PersistRecoveryInfo)) { + ret ^= *(p+1); + ret *= PRIME; + i++; + } + return ret; } LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) @@ -37,6 +45,7 @@ LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_ { index = -1; needRotate = true; + memset_s(&info, sizeof(info), 0, sizeof(info)); } LogPersisterRotator::~LogPersisterRotator() @@ -165,10 +174,10 @@ int LogPersisterRotator::SaveInfo(const LogPersistStartMsg& pMsg, const QueryCon void LogPersisterRotator::WriteRecoveryInfo() { std::cout << "Save Info file!" << std::endl; - uLong crc = GetInfoCRC32(info); + uint64_t hash = GetInfoHash(info); fseek(fdinfo, 0, SEEK_SET); fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); - fwrite(&crc, sizeof(uLong), 1, fdinfo); + fwrite(&hash, sizeof(hash), 1, fdinfo); fflush(fdinfo); fsync(fileno(fdinfo)); } diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 4ad8f1c..4b35009 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -597,12 +597,12 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) } PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); - uLong crcSum = 0L; - fread(&crcSum, sizeof(uLong), 1, infile); + uint64_t hashSum = 0L; + fread(&hashSum, sizeof(hashSum), 1, infile); fclose(infile); - uLong crc = GetInfoCRC32(info); - if (crc != crcSum) { - std::cout << "Info file CRC Checksum Failed!" << std::endl; + uint64_t hash = GetInfoHash(info); + if (hash != hashSum) { + std::cout << "Info file Checksum Failed!" << std::endl; continue; } JobLauncher(info.msg, _buffer, true, info.index + 1); -- Gitee