Skip to content

Commit

Permalink
Protect against duplicate assembly statement labels (#139)
Browse files Browse the repository at this point in the history
Make sure that there are no duplicate statement labels in assembly.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
  • Loading branch information
hawkinsw authored Jun 27, 2023
1 parent 78df87a commit 6bbc07a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/bpf_assembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef class _bpf_assembler
{"jsle", 0xd},
};

// Labels discoverd while parsing the assembly code.
// Labels discovered while parsing the assembly code.
std::unordered_map<std::string, size_t> _labels{};

// Vector of the same size as the assembly code, containing the label to
Expand Down Expand Up @@ -525,7 +525,13 @@ typedef class _bpf_assembler

// Check for labels.
if (mnemonic.ends_with(':')) {
_labels[mnemonic.substr(0, mnemonic.length() - 1)] = output.size();
auto label = mnemonic.substr(0, mnemonic.length() - 1);
if (_labels.contains(label)) {
std::stringstream ss{};
ss << "Duplicate label (" + label + ") detected at line " << output.size() << " (previous declaration at line " << _labels[label] << ")";
throw std::runtime_error(ss.str());
}
_labels[label] = output.size();
continue;
}

Expand Down

0 comments on commit 6bbc07a

Please sign in to comment.