feat(memory): add asan/lsan support, and formatting code. (#257)

* Modification item:
(1)Declare variables explicitly, and clarify variable types.
(2)Format the code style.
(3)Add asan support for memory address out of range checking.
(4)Add lsan support for memory leak checking.

* Simplify redirection semantics for easy understanding.

Co-authored-by: dinglei <libya_003@163.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3a56471..a2b178c 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -173,6 +173,20 @@
     string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")
     string(REPLACE ";" " " CMAKE_C_FLAGS "${C_FLAGS}")
 
+    option(ENABLE_ASAN "Enable asan reporting" OFF)
+    if (ENABLE_ASAN)
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -static-libasan")
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -static-libasan")
+        message(STATUS "** ENABLE_ASAN: ${ENABLE_ASAN} Enable asan reporting")
+    endif ()
+
+    option(ENABLE_LSAN "Enable lsan reporting" OFF)
+    if (ENABLE_LSAN)
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=leak -fno-omit-frame-pointer -static-liblsan")
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=leak -fno-omit-frame-pointer -static-liblsan")
+        message(STATUS "** ENABLE_LSAN: ${ENABLE_LSAN} Enable lsan reporting")
+    endif ()
+
     set(CMAKE_CXX_FLAGS_DEBUG "-O0 -DDEBUG")
     set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
 
diff --git a/build.sh b/build.sh
index 6979cd2..aa9b746 100755
--- a/build.sh
+++ b/build.sh
@@ -19,16 +19,16 @@
   cd $(dirname $0)
   pwd
 )
-down_dir="${basepath}/tmp_down_dir"
-build_dir="${basepath}/tmp_build_dir"
-packet_dir="${basepath}/tmp_packet_dir"
-install_lib_dir="${basepath}/bin"
-fname_libevent="libevent*.zip"
-fname_jsoncpp="jsoncpp*.zip"
-fname_boost="boost*.tar.gz"
-fname_libevent_down="release-2.1.11-stable.zip"
-fname_jsoncpp_down="0.10.7.zip"
-fname_boost_down="1.58.0/boost_1_58_0.tar.gz"
+declare down_dir="${basepath}/tmp_down_dir"
+declare build_dir="${basepath}/tmp_build_dir"
+declare packet_dir="${basepath}/tmp_packet_dir"
+declare install_lib_dir="${basepath}/bin"
+declare fname_libevent="libevent*.zip"
+declare fname_jsoncpp="jsoncpp*.zip"
+declare fname_boost="boost*.tar.gz"
+declare fname_libevent_down="release-2.1.11-stable.zip"
+declare fname_jsoncpp_down="0.10.7.zip"
+declare fname_boost_down="1.58.0/boost_1_58_0.tar.gz"
 
 PrintParams() {
   echo "=========================================one key build help============================================"
@@ -38,13 +38,15 @@
   echo ""
 }
 
-need_build_jsoncpp=1
-need_build_libevent=1
-need_build_boost=1
-test=0
-verbose=1
-codecov=0
-cpu_num=4
+declare need_build_jsoncpp=1
+declare need_build_libevent=1
+declare need_build_boost=1
+declare enable_asan=0
+declare enable_lsan=0
+declare verbose=1
+declare codecov=0
+declare cpu_num=4
+declare test=0
 
 pasres_arguments() {
   for var in "$@"; do
@@ -58,6 +60,12 @@
     noBoost)
       need_build_boost=0
       ;;
+    asan)
+      enable_asan=1
+      ;;
+    lsan)
+      enable_lsan=1
+      ;;
     noVerbose)
       verbose=0
       ;;
@@ -80,18 +88,26 @@
   else
     echo "need build libevent lib"
   fi
-
   if [ $need_build_jsoncpp -eq 0 ]; then
     echo "no need build jsoncpp lib"
   else
     echo "need build jsoncpp lib"
   fi
-
   if [ $need_build_boost -eq 0 ]; then
     echo "no need build boost lib"
   else
     echo "need build boost lib"
   fi
+  if [ $enable_asan -eq 1 ]; then
+    echo "enable asan reporting"
+  else
+    echo "disable asan reporting"
+  fi
+  if [ $enable_lsan -eq 1 ]; then
+    echo "enable lsan reporting"
+  else
+    echo "disable lsan reporting"
+  fi
   if [ $test -eq 1 ]; then
     echo "build unit tests"
   else
@@ -165,7 +181,7 @@
   else
     wget https://github.com/libevent/libevent/archive/${fname_libevent_down} -O libevent-${fname_libevent_down}
   fi
-  unzip -o ${fname_libevent} >unziplibevent.txt 2>&1
+  unzip -o ${fname_libevent} &> unziplibevent.txt
   if [ $? -ne 0 ]; then
     exit 1
   fi
@@ -181,7 +197,7 @@
   fi
   echo "build libevent static #####################"
   if [ $verbose -eq 0 ]; then
-    ./configure --disable-openssl --enable-static=yes --enable-shared=no CFLAGS=-fPIC CPPFLAGS=-fPIC --prefix=${install_lib_dir} >libeventconfig.txt 2>&1
+    ./configure --disable-openssl --enable-static=yes --enable-shared=no CFLAGS=-fPIC CPPFLAGS=-fPIC --prefix=${install_lib_dir} &> libeventconfig.txt
   else
     ./configure --disable-openssl --enable-static=yes --enable-shared=no CFLAGS=-fPIC CPPFLAGS=-fPIC --prefix=${install_lib_dir}
   fi
@@ -190,7 +206,7 @@
   fi
   if [ $verbose -eq 0 ]; then
     echo "build libevent without detail log."
-    make -j $cpu_num >libeventbuild.txt 2>&1
+    make -j $cpu_num &> libeventbuild.txt
   else
     make -j $cpu_num
   fi
@@ -214,7 +230,7 @@
   else
     wget https://github.com/open-source-parsers/jsoncpp/archive/${fname_jsoncpp_down} -O jsoncpp-${fname_jsoncpp_down}
   fi
-  unzip -o ${fname_jsoncpp} >unzipjsoncpp.txt 2>&1
+  unzip -o ${fname_jsoncpp} &> unzipjsoncpp.txt
   if [ $? -ne 0 ]; then
     exit 1
   fi
@@ -228,7 +244,7 @@
   echo "build jsoncpp static ######################"
   if [ $verbose -eq 0 ]; then
     echo "build jsoncpp without detail log."
-    cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${install_lib_dir} >jsoncppbuild.txt 2>&1
+    cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${install_lib_dir} &> jsoncppbuild.txt
   else
     cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${install_lib_dir}
   fi
@@ -236,7 +252,7 @@
     exit 1
   fi
   if [ $verbose -eq 0 ]; then
-    make -j $cpu_num >jsoncppbuild.txt 2>&1
+    make -j $cpu_num &> jsoncppbuild.txt
   else
     make -j $cpu_num
   fi
@@ -263,7 +279,7 @@
   else
     wget http://sourceforge.net/projects/boost/files/boost/${fname_boost_down}
   fi
-  tar -zxvf ${fname_boost} >unzipboost.txt 2>&1
+  tar -zxvf ${fname_boost} &> unzipboost.txt
   boost_dir=$(ls | grep ^boost | grep .*[^gz]$)
   cd ${boost_dir}
   if [ $? -ne 0 ]; then
@@ -277,7 +293,7 @@
   pwd
   if [ $verbose -eq 0 ]; then
     echo "build boost without detail log."
-    ./b2 -j$cpu_num cflags=-fPIC cxxflags=-fPIC --with-atomic --with-thread --with-system --with-chrono --with-date_time --with-log --with-regex --with-serialization --with-filesystem --with-locale --with-iostreams threading=multi link=static release install --prefix=${install_lib_dir} >boostbuild.txt 2>&1
+    ./b2 -j$cpu_num cflags=-fPIC cxxflags=-fPIC --with-atomic --with-thread --with-system --with-chrono --with-date_time --with-log --with-regex --with-serialization --with-filesystem --with-locale --with-iostreams threading=multi link=static release install --prefix=${install_lib_dir} &> boostbuild.txt
   else
     ./b2 -j$cpu_num cflags=-fPIC cxxflags=-fPIC --with-atomic --with-thread --with-system --with-chrono --with-date_time --with-log --with-regex --with-serialization --with-filesystem --with-locale --with-iostreams threading=multi link=static release install --prefix=${install_lib_dir}
   fi
@@ -289,18 +305,30 @@
 BuildRocketMQClient() {
   cd ${build_dir}
   echo "============start to build rocketmq client cpp.========="
-  if [ $test -eq 0 ]; then
-    cmake ..
-  else
+  local ROCKETMQ_CMAKE_FLAG=""
+  if [ $test -eq 1 ]; then
     if [ $codecov -eq 1 ]; then
-      cmake .. -DRUN_UNIT_TEST=ON -DCODE_COVERAGE=ON
+      ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DRUN_UNIT_TEST=ON -DCODE_COVERAGE=ON"
     else
-      cmake .. -DRUN_UNIT_TEST=ON
+      ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DRUN_UNIT_TEST=ON -DCODE_COVERAGE=OFF"
     fi
+  else
+      ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DRUN_UNIT_TEST=OFF -DCODE_COVERAGE=OFF"
   fi
+  if [ $enable_asan -eq 1 ]; then
+      ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DENABLE_ASAN=ON"
+  else
+      ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DENABLE_ASAN=OFF"
+  fi
+  if [ $enable_lsan -eq 1 ]; then
+      ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DENABLE_LSAN=ON"
+  else
+      ROCKETMQ_CMAKE_FLAG=$ROCKETMQ_CMAKE_FLAG" -DENABLE_LSAN=OFF"
+  fi
+  cmake .. $ROCKETMQ_CMAKE_FLAG
   if [ $verbose -eq 0 ]; then
     echo "build rocketmq without detail log."
-    make -j $cpu_num >buildclient.txt 2>&1
+    make -j $cpu_num &> buildclient.txt
   else
     make -j $cpu_num
   fi
@@ -317,12 +345,10 @@
     echo "no need build google test lib"
     return 0
   fi
-
   if [ -f ./bin/lib/libgtest.a ]; then
     echo "libgteest already exist no need build test"
     return 0
   fi
-
   cd ${down_dir}
   if [ -e release-1.8.1.tar.gz ]; then
     echo "${fname_boost} is exist"
@@ -330,15 +356,15 @@
     wget https://github.com/abseil/googletest/archive/release-1.8.1.tar.gz
   fi
   if [ ! -d "googletest-release-1.8.1" ]; then
-    tar -zxvf release-1.8.1.tar.gz >googletest.txt 2>&1
+    tar -zxvf release-1.8.1.tar.gz &> googletest.txt
   fi
   cd googletest-release-1.8.1
-  mkdir build
+  mkdir -p build
   cd build
   echo "build googletest static #####################"
   if [ $verbose -eq 0 ]; then
     echo "build googletest without detail log."
-    cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${install_lib_dir} >googletestbuild.txt 2>&1
+    cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${install_lib_dir} &> googletestbuild.txt
   else
     cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${install_lib_dir}
   fi
@@ -346,7 +372,7 @@
     exit 1
   fi
   if [ $verbose -eq 0 ]; then
-    make -j $cpu_num >gtestbuild.txt 2>&1
+    make -j $cpu_num &> gtestbuild.txt
   else
     make -j $cpu_num
   fi
diff --git a/distribution/deploy.sh b/distribution/deploy.sh
index c213d9a..d257c6d 100755
--- a/distribution/deploy.sh
+++ b/distribution/deploy.sh
@@ -40,7 +40,7 @@
 cp -rf ${CWD_DIR}/../LICENSE  ${DEPLOY_BUILD_HOME}/LICENSE
 cp -rf ${CWD_DIR}/../NOTICE ${DEPLOY_BUILD_HOME}/NOTICE
 
-cd ${CWD_DIR} && tar -cvzf ./${PKG_NAME}-${VERSION}-bin-release.tar.gz ./${PKG_NAME}  >/dev/null 2>&1
+cd ${CWD_DIR} && tar -cvzf ./${PKG_NAME}-${VERSION}-bin-release.tar.gz ./${PKG_NAME}  &> /dev/null
 rm -rf ${DEPLOY_BUILD_HOME}
 # # ##====================================================================
 #make clean
diff --git a/format.sh b/format.sh
index f46c5f2..7aa812e 100755
--- a/format.sh
+++ b/format.sh
@@ -26,7 +26,7 @@
 }
 
 #Setp1 check clang-format support
-if ! which clang-format &>/dev/null; then
+if ! which clang-format &> /dev/null; then
     echo -e "\033[32m !!!!!!please install clang-format  \033[0m"
     exit 1
 fi