0

I tried to solve Protostar heap2.

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

struct auth {
  char name[32];
  int auth;
};

struct auth *auth;
char *service;

int main(int argc, char **argv)
{
  char line[128];

  while(1) {
      printf("[ auth = %p, service = %p ]\n", auth, service);

      if(fgets(line, sizeof(line), stdin) == NULL) break;

      if(strncmp(line, "auth ", 5) == 0) {
          auth = malloc(sizeof(auth));
          memset(auth, 0, sizeof(auth));
          if(strlen(line + 5) < 31) {
              strcpy(auth->name, line + 5);
          }
      }
      if(strncmp(line, "reset", 5) == 0) {
          free(auth);
      }
      if(strncmp(line, "service", 6) == 0) {
          service = strdup(line + 7);
      }
      if(strncmp(line, "login", 5) == 0) {
          if(auth->auth) {
              printf("you have logged in already!\n");
          } else {
              printf("please enter your password\n");
          }
      }
  }
}

source code is like above. in malloc(sizeof(auth)); I thought parameter will be 36 or more. However result was 4.

0x080489a7 <main+115>:  movl   $0x4,(%esp)
---Type <return> to continue, or q <return> to quit---
0x080489ae <main+122>:  call   0x804916a <malloc>

This is assembly in gdb. I can't understand why parameter is 4. Can anyone explain why?

Damotorie
  • 586
  • 7
  • 25

0 Answers0