-2
void hexdump(void* ptr, const int buflen)
{
unsigned char* buf = (unsigned char*)ptr;
int i, j, d, hex = 0;
short* ins;
string op;
for (i = 0; i < buflen; i += 16) {
    for (j = 0; j < 16; j += 4) { 
        if (i + j < buflen) {
            cout << buflen << endl;
            cout << "inst " << (i+j) / 4 << ": ";

I was using linux ubuntu server. My purpose for programing is to read mechine code binary file and get the assembly code and print it out. However, above code is where failure is printed. Until cout << "inst " << (i+j) / 4 << ": "; it works, and buflen(which is 24) is printed but after that segmentation fault(core dumped) comes out and my execution stops. These are the rest of the code. (find,work functions are not yet made or used)

#include <fstream>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string find(char op[7]);
void work(string inst, short* ins);

void hexdump(void* ptr, const int buflen)
{
unsigned char* buf = (unsigned char*)ptr;
int i, j, d, hex = 0;
short* ins;
string op;
for (i = 0; i < buflen; i += 16) {
    for (j = 0; j < 16; j += 4) { 
        if (i + j < buflen) {
            cout << buflen << endl;
            cout << "inst " << (i+j) / 4 << ": ";
            for (int a = 0; a < 32; a += 8) {
                d = buf[i + j + a / 8];

                for (int k = 0; k < 8; k++) {
                    if (d % 2 != 0) {
                        ins[k + a] = 1;
                    }
                    else {
                        ins[k + a] = 0;
                    }
                    
                    d = d / 2;
                }
            }
            for (int i = 31; i >= 0; i -= 4) {
                hex = hex + ins[i] * 8;
                hex = hex + ins[i - 1] * 4;
                hex = hex + ins[i - 2] * 2;
                hex = hex + ins[i - 3] * 1;
                if (hex == 10)
                    printf("a");
                else if (hex == 11)
                    printf("b");
                else if (hex == 12)
                    printf("c");
                else if (hex == 13)
                    printf("d");
                else if (hex == 14)
                    printf("e");
                else if (hex == 15)
                    printf("f");
                else
                    printf("%d", hex);
                hex = 0;
            }
            for (int i = 6; i >=0; i--) {
                if (ins[i] == 1)
                    op.append("1");
                else if (ins[i] == 0)
                    op.append("0");
            }
            cout << endl << op << endl;
            //work(find(op), ins);
            printf("\n");
        }
    }
}
}

int main(int argc, char* argv[])
{
ifstream in;

in.open(argv[1], ios::in | ios::binary);
if (in.is_open())
{
    // get the starting position
    streampos start = in.tellg();

    // go to the end
    in.seekg(0, std::ios::end);

    // get the ending position
    streampos end = in.tellg();

    // go back to the start
    in.seekg(0, std::ios::beg);

    // create a vector to hold the data that
    // is resized to the total size of the file    
    std::vector<char> contents;
    contents.resize(static_cast<size_t>(end - start));

    // read it in
    in.read(&contents[0], contents.size());

    // print it out (for clarity)
    hexdump(contents.data(), contents.size());
}
in.close();
return 0;
}
string find(char op[7]) {
   string inst("unknown instruction");
   if(op=="")
   return inst;
}
void work(string inst, short* ins);
Adriaan
  • 17,741
  • 7
  • 42
  • 75
newb hi
  • 39
  • 5
  • (i + j < buflen) doesn't guarantee i + j + a / 8 < buflen, so you could have an out of bounds access. – Avi Berger Oct 03 '22 at 21:44
  • 2
    Have you tried stepping through the code with a debugger? Or at least inspecting the core dump? – Quimby Oct 03 '22 at 21:59
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 04 '22 at 01:45
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Oct 04 '22 at 01:45
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://meta.stackexchange.com/q/5221) – Adriaan Oct 07 '22 at 10:15
  • sorry.. I just thought my posting had too much useless info about specific things of my code – newb hi Oct 09 '22 at 09:13

1 Answers1

3

tldr: The variable ins is pointing to a random memory because the code never assigns it to anything valid. Hence, you have undefined behavior (crashing being the most likely outcome) when dereferencing this pointer and writing to it's address.

short* ins;   // THIS POINTER NEVER GETS ALLOCATED OR ASSIGNED TO VALID MEMORY
string op;
for (i = 0; i < buflen; i += 16) {
    for (j = 0; j < 16; j += 4) { 
        if (i + j < buflen) {
            cout << buflen << endl;
            cout << "inst " << (i+j) / 4 << ": ";
            for (int a = 0; a < 32; a += 8) {
                d = buf[i + j + a / 8];

                for (int k = 0; k < 8; k++) {
                    if (d % 2 != 0) {
                        ins[k + a] = 1;   // THIS IS UNDEFINED BEHAVIOR, IT PROBABLY CRASHES

selbie
  • 100,020
  • 15
  • 103
  • 173