lean4-htt/src/util/name_generator.cpp
Leonardo de Moura 13c532d0d4 fix(*): truncation bugs
- Lean strings (like std::string) may contain null characters. The
  codebase was ignoring this issue.

- We now have a wrapper `string_ref` for wrapping Lean string objects in
  C++. This wrapper also implements correctly the coercions std::string <-> string_ref.
  Remark: I also found a few places where the code relies on the
  following property which is not true
  Forall s : std::string, std::string(s.c_str()) == s

- `name` object wrapper was assuming that all numerals were small
  `nat` values. This is true in most cases, but the system would
  crash when processing if it is a big number.

- The commit tries to make sure runtime/util/kernel are correct.
  Modules that will be deleted contain many `TODO` comments
  indicating they may crash and/or produce incorrect results
  when strings contain null characters and numerals are big.

cc @kha

@kha: I thought about using `string` instead of `string_ref`.
We consistently use `std::string`. So, it should be fine, but I
was concerned about code readability.

After we bootstrap Lean4, we will be able to delete `lean::list`
template, and rename `lean::list_ref` to `lean::list`.

I am going to add `pair_ref` for wrapping Lean pair objects.
If we use `lean::string` instead of `lean::string_ref`, then
we should also use `lean::pair` instead of `lean::pair_ref`.
But, there is a problem in this case since we have
https://github.com/leanprover/lean4/blob/master/src/util/pair.h#L13
:(
2018-06-15 16:05:11 -07:00

101 lines
2.8 KiB
C++

/*
Copyright (c) 2018 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <limits>
#include <algorithm>
#include "runtime/sstream.h"
#include "util/name_generator.h"
#include "util/name_set.h"
namespace lean {
static name_set * g_ngen_prefixes = nullptr;
static name * g_tmp_prefix = nullptr;
name_generator::name_generator(name const & prefix):m_prefix(prefix), m_next_idx(0) {
lean_assert(!prefix.is_anonymous());
lean_assert(uses_name_generator_prefix(prefix));
}
name_generator::name_generator():name_generator(*g_tmp_prefix) {}
name name_generator::next() {
if (m_next_idx == std::numeric_limits<unsigned>::max()) {
// avoid overflow
m_prefix = name(m_prefix, m_next_idx);
m_next_idx = 0;
}
name r(m_prefix, m_next_idx);
m_next_idx++;
return r;
}
static name replace_base_prefix(name const & p, name const & new_base) {
if (g_ngen_prefixes->contains(p)) {
return new_base;
} else if (p.is_numeral()) {
return name(replace_base_prefix(p.get_prefix(), new_base), p.get_numeral());
} else if (p.is_string()) {
return name(replace_base_prefix(p.get_prefix(), new_base), p.get_string());
} else {
lean_unreachable();
}
}
name name_generator::next_with(name const & base_prefix) {
lean_assert(g_ngen_prefixes->contains(base_prefix));
return replace_base_prefix(next(), base_prefix);
}
void swap(name_generator & a, name_generator & b) {
swap(a.m_prefix, b.m_prefix);
std::swap(a.m_next_idx, b.m_next_idx);
}
void register_name_generator_prefix(name const & n) {
lean_assert(!g_ngen_prefixes->contains(n));
g_ngen_prefixes->insert(n);
}
bool uses_name_generator_prefix(name const & n) {
if (n.is_anonymous())
return false;
else if (g_ngen_prefixes->contains(n))
return true;
else
return uses_name_generator_prefix(n.get_prefix());
}
static void sanitize_name_generator_name(sstream & strm, name const & n) {
if (n.is_anonymous()) {
return;
} else if (n.is_numeral()) {
sanitize_name_generator_name(strm, n.get_prefix());
strm << "_" << n.get_numeral().to_std_string();
} else {
lean_assert(n.is_string());
sanitize_name_generator_name(strm, n.get_prefix());
strm << "_" << n.get_string().to_std_string();
}
}
name sanitize_name_generator_name(name const & n) {
if (!uses_name_generator_prefix(n))
return n;
sstream strm;
sanitize_name_generator_name(strm, n);
return name(strm.str().c_str());
}
void initialize_name_generator() {
g_ngen_prefixes = new name_set();
g_tmp_prefix = new name("_uniq");
register_name_generator_prefix(*g_tmp_prefix);
}
void finalize_name_generator() {
delete g_tmp_prefix;
delete g_ngen_prefixes;
}
}