Debugin UDI program by gdb

47 Views Asked by At

The program from below asks user for his name, greetings him and then gives us his real ID. We could assume that in case of 16-letter name (or longer) the uid variable will be overwritten and the program gives us incorrect user ID. But it isn't. How to explain this by using gdb?

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

struct user_info
{
    uid_t uid;
    char name[16];
};

int main(int argc, char *argv[])
{
    struct user_info info;
    info.uid = getuid();

    printf("Your name: ");
    scanf("%s", info.name);
    printf("Hello, %s!\nYour UID id %d.\n", info.name, (int) info.uid);
    return 0;
}
1

There are 1 best solutions below

0
On BEST ANSWER

Change the order of your struct like this

struct user_info
{
    char name[16];
    uid_t uid;
};

It will override as your expect.