Prevent leak after Ctrl + C when using read and malloc in loop

I need to make a project by creating a wrapper in C. There will be a while loop waiting for input characters to enter.

I believe that it is better to store the characters that we read in the pointer using mallocthan in a buffer (character array) with a fixed size, because we do not know how many characters will be sent. That way I could use reallocin a pointer to get a larger size if necessary.

I noticed with valgrind that when the program finished reading characters and waiting for new ones; if I press Ctrl + C, a memory leak will occur. The only solution I found to prevent this is to free the pointer after sending each command.

Is this a good idea or is there a better way to do this? For information, I read the characters in the buf buffer, and then concatenate the string to the str pointer. Here is the code:

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

#define READ_SIZE 1024

int main()
{
  char  buf[READ_SIZE];
  char  *str;
  int   ret;
  int   size;
  int   max;

  str = NULL;
  size = 0;
  max = READ_SIZE;
  write(1, "$> ", 3);
  while((ret = read(0, buf, READ_SIZE)))
    {
      if (str == NULL)
      {
        if ((str = malloc(READ_SIZE + 1)) == NULL)
          return EXIT_FAILURE;
        strncpy(str, buf, ret);
        str[ret] = '\0';
      }
      else
        str = strncat(str, buf, ret);
      if (strncmp(&buf[ret - 1], "\n", 1) == 0)
      {
        str[size + ret - 1] = '\0';
        printf("%s\n", str);
        size = 0;
        max = READ_SIZE;
        free(str);
        str = NULL;
        write(1, "$> ", 3);
      }
      else if (size + ret == max)
      {
        max *= 2;
        size += READ_SIZE;
        if ((str = realloc(str, max + 1)) == NULL)
          return EXIT_FAILURE;
      }
      else
        size += READ_SIZE;
    }
  free(str);
  return EXIT_SUCCESS;
}
+4
source share
1 answer

A few things to say here -

  • If it is important for your program to automatically exit to ctrl + C, you should add a signal handler to clear, and not exit immediately to ctrl + C. Note that your solution does not work correctly: if you press ctrl + c in split-second between your alloc and dealloc, you will still see a leak.
  • , , , . , , . SIGINT; , , SIGINT, . , .
  • ctrl + Z, .

- , , , , , ctrl + C, .

+4
source

All Articles