Skip to content

Commit a6ed95b

Browse files
committed
Improve VA sanitization & file size retrieval
Introduced sanitize_va() Introduced has_prefix() which now ensures "0x" prefix only when needed. Reduced unnecessary code repetition. Improved readability & efficiency.
1 parent 2f45c4f commit a6ed95b

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/rp/main.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ int main(int argc, char *argv[]) {
7878

7979
// Here we set the base being 0 if we want to have absolute virtual
8080
// memory address displayed
81-
const uint64_t base = g_opts.va.size() > 0
82-
? std::strtoull(g_opts.va.c_str(), nullptr, 0)
83-
: p.get_image_base_address();
81+
const uint64_t base = g_opts.va.size() > 0
82+
? std::strtoull((sanitize_va(g_opts.va)).c_str(), nullptr, 0)
83+
: p.get_image_base_address();
8484
if (g_opts.rop > 0) {
8585
const uint32_t options = g_opts.thumb ? 1 : 0;
8686
fmt::print("\nWait a few seconds, rp++ is looking for gadgets ({} "

src/rp/toolbox.cpp

+31-6
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,44 @@ std::string verbosity_to_string(const VerbosityLevel lvl) {
2828
return "Unknwon";
2929
}
3030

31+
// Get the size of an open file without changing its position
3132
std::streampos get_file_size(std::ifstream &file) {
32-
std::streampos backup = file.tellg();
3333

34-
file.seekg(0, std::ios::beg);
35-
std::streampos fsize = file.tellg();
34+
// Save the current file ptr position
35+
std::streampos backup = file.tellg();
3636

37+
// Move the ptr to the end
3738
file.seekg(0, std::ios::end);
38-
fsize = file.tellg() - fsize;
3939

40+
// Get the current file ptr position ( start = 0 + EOF )
41+
std::streampos fsize = file.tellg();
42+
43+
// Restore the previous ptr position
4044
file.seekg(backup);
45+
4146
return fsize;
4247
}
4348

49+
// Helper function to check if va has prefix (0x) or not
50+
bool has_prefix(const std::string &va) {
51+
return va.size() > 2 && va[0] == '0' && (va[1] == 'x' || va[1] == 'X');
52+
}
53+
54+
/*
55+
* Sanitize VA copied from WinDbg (removes backticks)
56+
* Ensures "0x" prefix exists when needed
57+
*/
58+
std::string sanitize_va(std::string va) {
59+
60+
bool needs_prefix = !has_prefix(va);
61+
62+
// Remove backticks if present
63+
va.erase(std::remove(va.begin(), va.end(), '`'), va.end());
64+
65+
return needs_prefix ? "0x" + va : va;
66+
67+
}
68+
4469
// this function is completely inspirated from the previous work of jonathan
4570
// salwan
4671
bool is_matching(const std::string &str, const std::string &pattern) {
@@ -49,10 +74,10 @@ bool is_matching(const std::string &str, const std::string &pattern) {
4974
return false;
5075
}
5176

52-
size_t i = 0, max = std::min(str.length(), pattern.length());
77+
size_t i = 0, maxLen = (std::min)(str.length(), pattern.length());
5378
bool it_matches = true;
5479

55-
while (i < max) {
80+
while (i < maxLen) {
5681
if (pattern.at(i) != '?' && pattern.at(i) != str.at(i)) {
5782
it_matches = false;
5883
break;

src/rp/toolbox.hpp

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ enum VerbosityLevel {
2525
*/
2626
std::string verbosity_to_string(const VerbosityLevel lvl);
2727

28+
/**
29+
* @brief Checks if the given virtual address has a "0x" or "0X" prefix.
30+
*
31+
* @param va The virtual address string.
32+
* @return true if the address has a "0x"/"0X" prefix, false otherwise.
33+
*/
34+
bool has_prefix(const std::string &va);
35+
36+
/**
37+
* @brief Sanitizes a virtual address copied from WinDbg.
38+
*
39+
* Removes backticks from the address and ensures it has a "0x" prefix if missing.
40+
*
41+
* @param va The virtual address string.
42+
* @return A sanitized virtual address with proper formatting.
43+
*/
44+
std::string sanitize_va(std::string va);
45+
2846
/**
2947
* \fn std::streampos get_file_size(std::ifstream &file)
3048
* \brief Get the size in byte of your file
@@ -33,6 +51,7 @@ std::string verbosity_to_string(const VerbosityLevel lvl);
3351
*
3452
* \return the size in byte of the file
3553
*/
54+
3655
std::streampos get_file_size(std::ifstream &file);
3756

3857
/**

0 commit comments

Comments
 (0)