Skip to content

Invalid char pointer deference behavior #165

Closed
@ChAoSUnItY

Description

@ChAoSUnItY

Considering the following code:

#include <stdlib.h>
#include <string.h>

char a[100];

typedef struct {
    char *raw;
} data_t;

void init_data(data_t *data, char *raw) {
    data->raw = raw;
}

int main() {
    strcpy(a, "DATA");
    data_t *data = malloc(sizeof(data_t));
    init_data(data, a);
    
    char *raw = data->raw;
    printf("%c\n", a[0]);
    printf("%c\n", raw[0]);
    printf("%c\n", data->raw[0]);
    
    
    free(data);

    return 0;
}

Using gcc to compile the program and run it, it would output:

D
D
D

But using shecc, it would output:

D
D
�

This happens because of the incorrect deference assembly code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions