提交 85edd52e 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!28 Fix Cpplint and improve security

Merge pull request !28 from 李鸿章/crc32_align
......@@ -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
......
......@@ -14,8 +14,7 @@
* limitations under the License.
*/
#include "crc32/crc32.h"
#include <stdint.h>
#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<const uint8_t *>(data);
......
......@@ -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 <cstddef>
#include <cstdint>
#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<const char*>(p));
}
inline uint32_t LE_LOAD32(const uint8_t* p) { return DecodeFixed32(reinterpret_cast<const char*>(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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册