浏览代码

fix mem access problem reported by sanitizer

yancey 2 年之前
父节点
当前提交
e66eddd1d5
共有 2 个文件被更改,包括 5 次插入5 次删除
  1. 1 1
      CMakeLists.txt
  2. 4 4
      common.cpp

+ 1 - 1
CMakeLists.txt

@@ -1,5 +1,5 @@
 #note: experimental
-#      currently only used for generating `compile_commands.json` for clangd
+#      currently only used for generating `compile_commands.json` for clangd.
 #      to build this project, it's suggested to use `makefile` instead
 
 cmake_minimum_required(VERSION 3.7)

+ 4 - 4
common.cpp

@@ -1127,8 +1127,8 @@ void print_binary_chars(const char *a, int len) {
 u32_t djb2(unsigned char *str, int len) {
     u32_t hash = 5381;
     int c;
-    int i = 0;
-    while (c = *str++, i++ != len) {
+    for (int i=0; i<len ;i++) {
+        c = *(str++);
         hash = ((hash << 5) + hash) ^ c; /* (hash * 33) ^ c */
     }
 
@@ -1139,8 +1139,8 @@ u32_t djb2(unsigned char *str, int len) {
 u32_t sdbm(unsigned char *str, int len) {
     u32_t hash = 0;
     int c;
-    int i = 0;
-    while (c = *str++, i++ != len) {
+    for (int i=0; i<len ;i++) {
+        c = *(str++);
         hash = c + (hash << 6) + (hash << 16) - hash;
     }
     // hash=htonl(hash);