diff --git a/build/scripts/crc32.sh b/build/scripts/crc32.sh index f15730971a2a26bff80bf85068fae2e19bf753af..b37eb13f51e44687e31421d62bc176b47624169f 100755 --- a/build/scripts/crc32.sh +++ b/build/scripts/crc32.sh @@ -63,13 +63,11 @@ build_crc32() { rm -f "$CRC32_SOURCE_DIR/$CRC32_SO_FILE" rm -f "$CRC32_OUTPUT_DIR/$CRC32_SO_FILE" - PYBIND11_INCLUDES=$($PYTHON -m pybind11 --includes) - PYTHON_INCLUDE=$(echo "$PYBIND11_INCLUDES" | awk '{print $1}' | sed "s/^-I//g") - PYTHON_HEADERS=$(echo "$PYBIND11_INCLUDES" | awk '{print $2}' | sed "s/^-I//g") + read -ra PYBIND11_INCLUDES <<<"$($PYTHON -m pybind11 --includes)" c++ -O2 -O3 -shared -std=c++11 -fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 \ -Wno-maybe-uninitialized -Wno-unused-parameter -Wall -Wl,-z,relro,-z,now,-z,noexecstack \ - -I"$THIRD_PARTY_DIR" -I"$DATAVISUAL_DIR/utils" -I"$PYTHON_INCLUDE" -I"$PYTHON_HEADERS" \ + -I"$THIRD_PARTY_DIR/securec/include" "${PYBIND11_INCLUDES[0]}" "${PYBIND11_INCLUDES[1]}" \ -o "$CRC32_SO_FILE" crc32.cc "$BUILDDIR/libsecurec.a" if [ ! -f "$CRC32_SO_FILE" ]; then diff --git a/mindinsight/datavisual/utils/crc32/crc32.cc b/mindinsight/datavisual/utils/crc32/crc32.cc index 5e9ae853bbedfa0ca9a23a7ee6129bc7f45a65cf..098e329896693676436e7d8052d547a7c6e34e93 100644 --- a/mindinsight/datavisual/utils/crc32/crc32.cc +++ b/mindinsight/datavisual/utils/crc32/crc32.cc @@ -14,8 +14,7 @@ * limitations under the License. */ -#include "crc32/crc32.h" -#include +#include "crc32.h" static const uint32_t crc_table_o32[CRC_TABLE_SIZE] = { @@ -280,9 +279,8 @@ inline void CRC32T8(uint32_t *crc, const uint8_t **p) { // calc the crc32c value uint32_t MakeCrc32c(uint32_t init_crc, const char *data, size_t size) { - EXCEPT_CHECK_NULL(data); uint32_t crc = init_crc ^ 0xFFFFFFFFU; - const long OFFSET = 8; + const int OFFSET = 8; // Get the origin begin and end address(not alignment) auto *bp = reinterpret_cast(data); diff --git a/mindinsight/datavisual/utils/crc32/crc32.h b/mindinsight/datavisual/utils/crc32/crc32.h index 388d9982c61e54a6dfc887dbc7170d56216368d8..4f8d9d0b170dd0e3274020f4e05d61f01df37aac 100644 --- a/mindinsight/datavisual/utils/crc32/crc32.h +++ b/mindinsight/datavisual/utils/crc32/crc32.h @@ -17,10 +17,10 @@ #ifndef DATAVISUAL_UTILS_CRC32_CRC32_H_ #define DATAVISUAL_UTILS_CRC32_CRC32_H_ -#include "pybind11/pybind11.h" -#include "securec/include/securec.h" #include #include +#include "pybind11/pybind11.h" +#include "securec.h" #define CRC_TABLE_SIZE 256 #define RIGHT_SHIFT 15 @@ -29,28 +29,19 @@ // Align n to (1 << m) byte boundary #define MEM_ALIGN(n, m) ((n + ((1 << m) - 1)) & ~((1 << m) - 1)) -// check the null point, Only log it in if(): The value is null -#define EXCEPT_CHECK_NULL(value) \ - do { \ - if (value == nullptr) { \ - break; \ - } \ - } while (0) - // implement common define function // Get the 32 bits align value inline uint32_t DecodeFixed32(const char* ptr) { uint32_t result = 0; - if(EOK != memcpy_s(&result, sizeof(result), ptr, sizeof(result))) { - return result; + if (EOK != memcpy_s(&result, sizeof(result), ptr, sizeof(result))) { + // `0` indicates that something wrong happened + return 0; } return result; } // Used to fetch a naturally-aligned 32-bit word in little endian byte-order -inline uint32_t LE_LOAD32(const uint8_t* p) { - return DecodeFixed32(reinterpret_cast(p)); -} +inline uint32_t LE_LOAD32(const uint8_t* p) { return DecodeFixed32(reinterpret_cast(p)); } // Masked for crc. static constexpr uint32_t kMaskDelta = 0xA282EAD8U; @@ -62,6 +53,10 @@ uint32_t MakeCrc32c(uint32_t init_crc, const char* data, size_t size); // A function return the crc32c value uint32_t GetMaskCrc32cValue(const char* data, size_t n) { + if (data == nullptr && n > 0) { + // Return early to prevent MakeCrc32c resulting in segmentfault + return 0; + } uint32_t crc = MakeCrc32c(0, data, n); return ((crc >> RIGHT_SHIFT) | (crc << LEFT_SHIFT)) + kMaskDelta; }