Skip to content

Commit 03375cf

Browse files
committed
Fix line number off-by-one errors
1 parent 2137b6d commit 03375cf

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/e9tool/e9dwarf.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ extern const Line *e9tool::findLine(const Lines &Ls, intptr_t addr)
243243
auto i = Ls.lower_bound(addr);
244244
if (i == Ls.end())
245245
return nullptr;
246-
return &i->second;
246+
if (i->second.lb == addr)
247+
return &i->second;
248+
auto j = Ls.begin();
249+
if (i == j)
250+
return nullptr;
251+
i--;
252+
if (i->second.lb <= addr && addr < i->second.ub)
253+
return &i->second;
254+
else
255+
return nullptr;
247256
}
248257

src/e9tool/e9metadata.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,12 +1957,12 @@ static Type sendLoadArgumentMetadata(FILE *out, CallInfo &info,
19571957
case ARGUMENT_FILENAME: case ARGUMENT_ABSNAME:
19581958
case ARGUMENT_BASENAME: case ARGUMENT_DIRNAME:
19591959
{
1960-
auto i = elf->lines.lower_bound(I->address);
1961-
if (i != elf->lines.end())
1960+
const Line *line = findLine(elf->lines, I->address);
1961+
if (line != nullptr)
19621962
{
1963-
const auto &line = i->second;
19641963
if (arg.kind == ARGUMENT_DIRNAME &&
1965-
(line.dir == nullptr && strchr(line.file, '/') == nullptr))
1964+
(line->dir == nullptr &&
1965+
strchr(line->file, '/') == nullptr))
19661966
{
19671967
sendSExtFromI32ToR64(out, 0, regno);
19681968
t = TYPE_NULL_PTR;
@@ -1991,21 +1991,20 @@ static Type sendLoadArgumentMetadata(FILE *out, CallInfo &info,
19911991
}
19921992
case ARGUMENT_LINE:
19931993
{
1994-
auto i = elf->lines.lower_bound(I->address);
1995-
if (i == elf->lines.end())
1994+
const Line *line = findLine(elf->lines, I->address);
1995+
if (line == nullptr)
19961996
{
19971997
sendSExtFromI32ToR64(out, 0, regno);
19981998
t = TYPE_CONST_VOID_PTR;
19991999
break;
20002000
}
2001-
const Line &line = i->second;
20022001
switch (arg.field)
20032002
{
20042003
case FIELD_NONE:
2005-
sendLoadValueMetadata(out, line.line, regno);
2004+
sendLoadValueMetadata(out, line->line, regno);
20062005
break;
20072006
case FIELD_SIZE:
2008-
sendLoadValueMetadata(out, line.ub - line.lb, regno);
2007+
sendLoadValueMetadata(out, line->ub - line->lb, regno);
20092008
break;
20102009
default:
20112010
error("unknown field (%d)", arg.field);
@@ -2091,21 +2090,20 @@ static void sendArgumentDataMetadata(FILE *out, const char *name,
20912090
case ARGUMENT_FILENAME: case ARGUMENT_ABSNAME:
20922091
case ARGUMENT_BASENAME: case ARGUMENT_DIRNAME:
20932092
{
2094-
auto i = elf->lines.lower_bound(I->address);
2095-
if (i == elf->lines.end())
2093+
const Line *line = findLine(elf->lines, I->address);
2094+
if (line == nullptr)
20962095
return;
2097-
const auto &line = i->second;
20982096
std::string tmp;
2099-
const char *file = line.file, *kind = "file";
2097+
const char *file = line->file, *kind = "file";
21002098
switch (arg.kind)
21012099
{
21022100
case ARGUMENT_ABSNAME:
2103-
getAbsname(line.dir, line.file, tmp);
2101+
getAbsname(line->dir, line->file, tmp);
21042102
kind = "abs"; file = tmp.c_str(); break;
21052103
case ARGUMENT_BASENAME:
2106-
kind = "base"; file = getBasename(line.file); break;
2104+
kind = "base"; file = getBasename(line->file); break;
21072105
case ARGUMENT_DIRNAME:
2108-
getDirname(line.dir, line.file, tmp);
2106+
getDirname(line->dir, line->file, tmp);
21092107
kind = "dir"; file = tmp.c_str(); break;
21102108
default:
21112109
break;

0 commit comments

Comments
 (0)