Skip to content
This repository has been archived by the owner on Feb 4, 2025. It is now read-only.

Struct rvals are dereferenced too many times #103

Closed
jyn514 opened this issue Oct 28, 2019 · 1 comment · Fixed by #138
Closed

Struct rvals are dereferenced too many times #103

jyn514 opened this issue Oct 28, 2019 · 1 comment · Fixed by #138
Labels
bug Something isn't working parser Issue to do with parsing the abstract syntax tree structs Struct errors are the most common lately by far
Milestone

Comments

@jyn514
Copy link
Owner

jyn514 commented Oct 28, 2019

Describe the bug
Structs are dereferenced one too many times

Expected behavior
A struct deref (get_struct()->m) performs one deref.

Actual Behavior
Struct derefs perform two derefs.

Code

$ cargo run
typedef unsigned int __uid_t;
extern __uid_t getuid (void) ;

struct passwd {
  char *pw_name;
  char *pw_passwd;
};

extern struct passwd *getpwuid (__uid_t __uid);
extern int puts (const char *__s);

int main(void) {
  puts(getpwuid(getuid())->pw_name);
}
$ ./a.out
Segmentation fault
AST

Please paste the output of cargo run -- --debug-ast here.

typedef unsigned int __uid_t;
extern __uid_t getuid (void) ;

struct passwd {
  char *pw_name;
  char *pw_passwd;
};

extern struct passwd *getpwuid (__uid_t __uid);
extern int puts (const char *__s);

int main(void) {
  puts(getpwuid(getuid())->pw_name);
}
extern unsigned int getuid(void);
extern unsigned int getuid(void);
extern struct passwd * getpwuid(unsigned int);
extern struct passwd * getpwuid(unsigned int);
extern int puts(char *);
auto int main(void) {
(puts)(*((*((getpwuid)((getuid)()))).pw_name));
}
ASM
function u0:0() -> i32 system_v {
    sig0 = () -> i32 system_v
    sig1 = (i32) -> i64 system_v
    sig2 = (i64) -> i32 system_v
    fn0 = u0:0 sig0
    fn1 = u0:1 sig1
    fn2 = u0:2 sig2

ebb0:
    v0 = call fn0()
    v1 = call fn1(v0)
    v2 = load.i64 v1
    v3 = iconst.i64 0
    v4 = iadd v2, v3
    v5 = load.i64 v4
    v6 = call fn2(v5)
    v7 = iconst.i32 0
    return v7
}
@jyn514 jyn514 added bug Something isn't working parser Issue to do with parsing the abstract syntax tree structs Struct errors are the most common lately by far labels Oct 28, 2019
@jyn514 jyn514 added this to the MVP milestone Oct 28, 2019
@jyn514
Copy link
Owner Author

jyn514 commented Oct 28, 2019

This can be worked around by putting the struct assignment on its own line:

struct passwd *p = getpwuid(getuid());
puts(p->pw_name);

Maybe our code assumes that structs are always stored in a variable?

@jyn514 jyn514 changed the title Structs are dereferenced too many times Struct rvals are dereferenced too many times Oct 28, 2019
jyn514 added a commit that referenced this issue Dec 4, 2019
jyn514 added a commit that referenced this issue Dec 4, 2019
jyn514 added a commit that referenced this issue Feb 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working parser Issue to do with parsing the abstract syntax tree structs Struct errors are the most common lately by far
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant