Fixed many hardening issues
This commit is contained in:
45
toolbox.cpp
45
toolbox.cpp
@@ -1,24 +1,11 @@
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
|
||||
#include "toolbox.hpp"
|
||||
|
||||
namespace asp {
|
||||
/**
|
||||
* @brief Swap two given memory values
|
||||
*
|
||||
* @tparam T Type of memory placeholder
|
||||
* @param a Firat memory pointer
|
||||
* @param b Second memory pointer
|
||||
*/
|
||||
template<typename T>
|
||||
constexpr void swap(T* const a, T* const b) noexcept {
|
||||
const T temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert a given number to string
|
||||
*
|
||||
@@ -31,12 +18,12 @@ namespace asp {
|
||||
size_t i = 0;
|
||||
|
||||
for (; num > 0; num /= 10)
|
||||
str[offset + i++] = num % 10 + '0';
|
||||
str[offset + i++] = char(num % 10) + '0';
|
||||
|
||||
str[offset + i] = '\0';
|
||||
|
||||
for (size_t j = 0; j < i / 2; ++j)
|
||||
swap(str + offset + j, str + offset + i - j - 1);
|
||||
std::swap(str[offset + j], str[offset + i - j - 1]);
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -49,7 +36,7 @@ namespace asp {
|
||||
*/
|
||||
template<typename T>
|
||||
static constexpr uint64_t u64(const T var) noexcept {
|
||||
return static_cast<uint64_t>(var);
|
||||
return uint64_t(var);
|
||||
}
|
||||
|
||||
static constexpr const size_t STR_BUFFER_SIZE = 64;
|
||||
@@ -77,10 +64,10 @@ namespace asp {
|
||||
}
|
||||
|
||||
size_t j = 0;
|
||||
for(int8_t i = static_cast<int8_t>(format_prefix.size() - 1) * 10; i >= 0; i -= 10){
|
||||
for(uint8_t i = uint8_t(format_prefix.size() - 1) * 10; i > 0; i -= 10){
|
||||
const uint64_t nsi = n >> i;
|
||||
if(nsi > 0){
|
||||
const int8_t idx = i / 10;
|
||||
const uint8_t idx = i / 10;
|
||||
j += ullstr(nsi, j, s);
|
||||
for(int k = 0; format_prefix[idx][k] > 0; ++k)
|
||||
s[j++] = format_prefix[idx][k];
|
||||
@@ -88,6 +75,12 @@ namespace asp {
|
||||
n &= u64(-1) >> (64 - i);
|
||||
}
|
||||
}
|
||||
if(n > 0){
|
||||
j += ullstr(n, j, s);
|
||||
for(int k = 0; format_prefix[0][k] > 0; ++k)
|
||||
s[j++] = format_prefix[0][k];
|
||||
s[j++] = ' ';
|
||||
}
|
||||
|
||||
/* Remove trailing character */
|
||||
s[j - 1] = '\0';
|
||||
@@ -111,7 +104,7 @@ namespace asp {
|
||||
}
|
||||
|
||||
uint64_t res;
|
||||
for (int8_t i = time_numbers.size() - 1; i >= 0; --i) {
|
||||
for (uint8_t i = time_numbers.size() - 1; i > 0; --i) {
|
||||
if (time >= time_numbers[i]) {
|
||||
res = time / time_numbers[i];
|
||||
time %= time_numbers[i];
|
||||
@@ -121,6 +114,14 @@ namespace asp {
|
||||
s[j++] = ' ';
|
||||
}
|
||||
}
|
||||
if (time >= time_numbers[0]) {
|
||||
res = time / time_numbers[0];
|
||||
time %= time_numbers[0];
|
||||
j += ullstr(res, j, s);
|
||||
for(int k = 0; time_formats[0][k] > 0; ++k)
|
||||
s[j++] = time_formats[0][k];
|
||||
s[j++] = ' ';
|
||||
}
|
||||
|
||||
/* Remove trailing character */
|
||||
s[j - 1] = '\0';
|
||||
|
Reference in New Issue
Block a user