Skip to content

Commit

Permalink
Fix Andersbakken#717. Don't use sscanf.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersbakken committed Jun 13, 2016
1 parent 91aabd3 commit 0eccb78
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/Location.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,35 +198,50 @@ class Location
error("%s:%d:%d is not indexed", path.constData(), line, col);
return Location();
}
static bool parse(const String &key, const Path &pwd, Path::ResolveMode mode,
uint32_t *line, uint32_t *col, Path *path)
{
const size_t lastColon = key.lastIndexOf(':', key.size() - 2);
if (lastColon == String::npos)
return false;
const size_t secondLastColon = key.lastIndexOf(':', lastColon - 1);
if (secondLastColon == String::npos)
return false;
const char *str = key.constData();
char *end;
assert(line);
*line = static_cast<uint32_t>(strtoul(str + secondLastColon + 1, &end, 10));
if (*end != ':' || end == str + secondLastColon + 1)
return false;
assert(col);
*col = static_cast<uint32_t>(strtoul(str + lastColon + 1, &end, 10));
if (*end != ':' || end == str + lastColon + 1)
return false;

*path = Path::resolved(key.left(secondLastColon), mode, pwd);
return path->isFile();
}
static String encode(const String &key, const Path &pwd = Path())
{
char path[PATH_MAX];
uint32_t line, col;
if (sscanf(key.constData(), "%[^':']:%u:%u", path, &line, &col) != 3)
return String();

Path resolved = Path::resolved(path, Path::MakeAbsolute, pwd);
if (!resolved.isFile())
Path path;
if (!parse(key, pwd, Path::MakeAbsolute, &line, &col, &path))
return String();
{
char buf[8];
memcpy(buf, &line, sizeof(line));
memcpy(buf + 4, &col, sizeof(col));
resolved.append(buf, 8);
}

return resolved;
char buf[8];
memcpy(buf, &line, sizeof(line));
memcpy(buf + 4, &col, sizeof(col));
path.append(buf, 8);
return path;
}

static Location fromPathLineAndColumn(const String &str, const Path &pwd = Path())
{
char path[PATH_MAX];
uint32_t line, col;
if (sscanf(str.constData(), "%[^':']:%u:%u", path, &line, &col) != 3)
Path path;
if (!parse(str, pwd, Path::RealPath, &line, &col, &path))
return Location();

const Path resolved = Path::resolved(path, Path::RealPath, pwd);
const uint32_t fileId = Location::fileId(resolved);
const uint32_t fileId = Location::fileId(path);
if (!fileId)
return Location();
return Location(fileId, line, col);
Expand Down

0 comments on commit 0eccb78

Please sign in to comment.