Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef OPENZIM_LIBZIM_TOOLS_H
#define OPENZIM_LIBZIM_TOOLS_H

#include <cstddef> // size_t
#include <cstring> // memcmp
#include <string>
#include <tuple>
#include <map>
Expand Down Expand Up @@ -71,6 +73,17 @@ namespace zim {
return count;
}

// Work around for errors reported by too picky UB sanitizer tools on std::memcmp()
// being called with a nullptr pointer argument even though count==0. Explicit
// nullptr checks to satisfy other picky compilers.
template <typename T>
int safe_memcmp(T const *const lhs, T const *const rhs, std::size_t count) noexcept {
if(count == 0UL || lhs == nullptr || rhs == nullptr) {
return 0;
}
return std::memcmp(lhs, rhs, count);
}

namespace writer {
class Dirent;
bool isFrontArticle(const Dirent* dirent, const Hints& hints);
Expand Down
7 changes: 4 additions & 3 deletions src/writer/tinyString.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef ZIM_WRITER_TINYSTRING_H
#define ZIM_WRITER_TINYSTRING_H

#include "../tools.h"
#include "../zim_types.h"
#include <cstring>

Expand Down Expand Up @@ -66,11 +67,11 @@ namespace zim
size_t size() const { return m_size; }
const char* const data() const { return m_data; }
bool operator==(const TinyString& other) const {
return (m_size == other.m_size) && (std::memcmp(m_data, other.m_data, m_size) == 0);
return (m_size == other.m_size) && (safe_memcmp(m_data, other.m_data, m_size) == 0);
}
bool operator<(const TinyString& other) const {
auto min_size = std::min(m_size, other.m_size);
auto ret = std::memcmp(m_data, other.m_data, min_size);
const auto min_size = std::min(m_size, other.m_size);
const auto ret = safe_memcmp(m_data, other.m_data, min_size);
if (ret == 0) {
return m_size < other.m_size;
} else {
Expand Down