类似Getline的函数崩溃

时间:2018-01-17 17:03:44

标签: c crash malloc valgrind free

我目前正在学习编程,我在第一年就读。我们目前正在学习C,我们的任务是创建一个类似于 getline()的函数,但不同之处在于该函数仅将文件描述符作为参数并在每次调用时返回 malloc' d 字符串,该字符串表示文件描述符对应的文件中的下一行。

该函数返回的行不得包含任何' \ n' 字符,当没有任何内容可供阅读时,必须返回 NULL 函数遇到错误。我们还限于以下功能:读取,malloc和免费。

我已经写了下面的代码(对不起,如果它很难读,我还在学习,我们有编码风格"在编写代码时要遵循):

#include "get_next_line.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int find_n(char *str)
{
    int i = 0;

    while (str[i] != '\0') {
        if (str[i] == '\n')
            return (i);
        i++;
    }
    return (0);
}

static char *my_realloc(char *str, unsigned int size, unsigned int start_at)
{
    int i = 0;
    char *new_str;

    new_str = malloc(size);
    while (str[i + start_at] != '\0') {
        new_str[i] = str[i + start_at];
        i++;
    }
    if (str[i + start_at] == '\0')
        new_str[i] ==  '\0';
    free(str);
    return (new_str);
}

static char *get_line(char *str, unsigned int *total_read)
{
    int i = find_n(str);
    int j = 0;
    int len = 0;
    char *line = malloc(i + 1);

    while (j < i) {
        line[j] = str[j];
        j++;
    }
    line[j] = '\0';
    while (str[i + 1 + len] != '\0') {
        len++;
    }
    *total_read = len;
    str = my_realloc(str, len + 1, i + 1);
    return (line);
}

static void my_strcat(char *dest, char const *src)
{
    int dest_len = 0;
    int i = 0;

    while (dest[i] != '\0') {
        i++;
    }
    dest_len = i;
    i = 0;
    while (src[i] != '\0') {
        dest[dest_len + i] = src[i];
        i++;
    }
    dest[dest_len + i] = '\0';
}

static char *get_text(int fd, char *str)
{
    char *buf;
    int n_read = READ_SIZE;
    static unsigned int total_read = 0;
    int loop_started = 0;

    buf = malloc(READ_SIZE + 1);
    buf[0] = '\0';
    while (n_read == READ_SIZE) {
        n_read = read(fd, buf, READ_SIZE);
        if (n_read == -1 || (n_read == 0 && !loop_started)) {
            free(str);
            free(buf);
            return (NULL);
        }
        buf[n_read] = '\0';
        total_read += n_read;
        str = my_realloc(str, total_read + 1, 0);
        my_strcat(str, buf);
        loop_started = 1;
        if (find_n(buf)) {
            break;
        }
    }
    free(buf);
    return (get_line(str, &total_read));
}

char *get_next_line(int fd)
{
    static char *str;
    int is_str_malloced = 0;

    if (!is_str_malloced) {
        str = malloc(1);
        str[0] = '\0';
        is_str_malloced = 1;
    }
    return(get_text(fd, str));
}

int main(void)
{
    char *str;
    int fd;

    fd = open("script", O_RDONLY);
    while (1) {
        str = get_next_line(fd);
        if (str == NULL) {
            close(fd);
            return (0);
        }
        printf("%s\n", str);
    }
    close(fd);
    return (0);
}

主要功能几乎仅用于测试目的。当我编译并测试它时,我得到:

*** Error in `./a.out': free(): invalid next size (normal): 0x0000000000ed71b0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7cbac)[0x7f67d51a5bac]
/lib64/libc.so.6(+0x87a59)[0x7f67d51b0a59]
/lib64/libc.so.6(cfree+0x16e)[0x7f67d51b63be]
./a.out[0x40071b]
./a.out[0x40095f]
./a.out[0x400a07]
./a.out[0x400a32]
/lib64/libc.so.6(__libc_start_main+0xea)[0x7f67d514988a]
./a.out[0x40059a]
======= Memory map: ========
00400000-00401000 r-xp 00000000 103:05 22939320                          /home/ldidierjean/delivery/CPE_getnextline_2017/a.out
00600000-00601000 r--p 00000000 103:05 22939320                          /home/ldidierjean/delivery/CPE_getnextline_2017/a.out
00601000-00602000 rw-p 00001000 103:05 22939320                          /home/ldidierjean/delivery/CPE_getnextline_2017/a.out
00ed7000-00ef8000 rw-p 00000000 00:00 0                                  [heap]
7f67d0000000-7f67d0021000 rw-p 00000000 00:00 0 
7f67d0021000-7f67d4000000 ---p 00000000 00:00 0 
7f67d4f12000-7f67d4f28000 r-xp 00000000 103:05 7210485                   /usr/lib64/libgcc_s-7-20170915.so.1
7f67d4f28000-7f67d5127000 ---p 00016000 103:05 7210485                   /usr/lib64/libgcc_s-7-20170915.so.1
7f67d5127000-7f67d5128000 r--p 00015000 103:05 7210485                   /usr/lib64/libgcc_s-7-20170915.so.1
7f67d5128000-7f67d5129000 rw-p 00016000 103:05 7210485                   /usr/lib64/libgcc_s-7-20170915.so.1
7f67d5129000-7f67d52f4000 r-xp 00000000 103:05 7217643                   /usr/lib64/libc-2.25.so
7f67d52f4000-7f67d54f4000 ---p 001cb000 103:05 7217643                   /usr/lib64/libc-2.25.so
7f67d54f4000-7f67d54f8000 r--p 001cb000 103:05 7217643                   /usr/lib64/libc-2.25.so
7f67d54f8000-7f67d54fa000 rw-p 001cf000 103:05 7217643                   /usr/lib64/libc-2.25.so
7f67d54fa000-7f67d54fe000 rw-p 00000000 00:00 0 
7f67d54fe000-7f67d5525000 r-xp 00000000 103:05 7219338                   /usr/lib64/ld-2.25.so
7f67d56f6000-7f67d56f9000 rw-p 00000000 00:00 0 
7f67d5721000-7f67d5724000 rw-p 00000000 00:00 0 
7f67d5724000-7f67d5725000 r--p 00026000 103:05 7219338                   /usr/lib64/ld-2.25.so
7f67d5725000-7f67d5727000 rw-p 00027000 103:05 7219338                   /usr/lib64/ld-2.25.so
7fff51a9b000-7fff51abd000 rw-p 00000000 00:00 0                          [stack]
7fff51ac2000-7fff51ac5000 r--p 00000000 00:00 0                          [vvar]
7fff51ac5000-7fff51ac7000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Abandon (core dumped)

Valgrind给了我:

==6142== Memcheck, a memory error detector
==6142== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6142== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6142== Command: ./a.out --track-origins=yes
==6142== 
==6142== Conditional jump or move depends on uninitialised value(s)
==6142==    at 0x400824: my_strcat (get_next_line.c:67)
==6142==    by 0x400960: get_text (get_next_line.c:98)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142== 
==6142== Invalid read of size 1
==6142==    at 0x40081F: my_strcat (get_next_line.c:67)
==6142==    by 0x400960: get_text (get_next_line.c:98)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142==  Address 0x5214257 is 0 bytes after a block of size 16,759 alloc'd
==6142==    at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142==    by 0x4006B3: my_realloc (get_next_line.c:31)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142== 
==6142== Invalid write of size 1
==6142==    at 0x400857: my_strcat (get_next_line.c:73)
==6142==    by 0x400960: get_text (get_next_line.c:98)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142==  Address 0x5214270 is 16 bytes after a block of size 16,768 in arena "client"
==6142== 
==6142== Invalid write of size 1
==6142==    at 0x400883: my_strcat (get_next_line.c:76)
==6142==    by 0x400960: get_text (get_next_line.c:98)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142==  Address 0x5214277 is 23 bytes after a block of size 16,768 in arena "client"
==6142== 
==6142== Conditional jump or move depends on uninitialised value(s)
==6142==    at 0x4006F7: my_realloc (get_next_line.c:32)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142== 
==6142== Invalid read of size 1
==6142==    at 0x4006F2: my_realloc (get_next_line.c:32)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142==  Address 0x5214257 is 0 bytes after a block of size 16,759 alloc'd
==6142==    at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142==    by 0x4006B3: my_realloc (get_next_line.c:31)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142== 
==6142== Invalid read of size 1
==6142==    at 0x4006D8: my_realloc (get_next_line.c:33)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142==  Address 0x5214257 is 0 bytes after a block of size 16,759 alloc'd
==6142==    at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142==    by 0x4006B3: my_realloc (get_next_line.c:31)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142== 
==6142== Invalid write of size 1
==6142==    at 0x4006DB: my_realloc (get_next_line.c:33)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142==  Address 0x521841e is 0 bytes after a block of size 16,766 alloc'd
==6142==    at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142==    by 0x4006B3: my_realloc (get_next_line.c:31)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142== 
==6142== Invalid read of size 1
==6142==    at 0x40070A: my_realloc (get_next_line.c:36)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)
==6142==  Address 0x5214277 is 23 bytes after a block of size 16,768 in arena "client"
==6142== 
--6142-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
--6142-- si_code=1;  Faulting address: 0x206991B288;  sp: 0x1002ba9e30

valgrind: the 'impossible' happened:
   Killed by fatal signal

host stacktrace:
==6142==    at 0x5804F2DC: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
==6142==    by 0x5800B304: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
==6142==    by 0x5800B4D2: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
==6142==    by 0x58098653: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
==6142==    by 0x580A7256: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)

sched status:
  running_tid=1

Thread 1: status = VgTs_Runnable (lwpid 6142)
==6142==    at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142==    by 0x4006B3: my_realloc (get_next_line.c:31)
==6142==    by 0x400949: get_text (get_next_line.c:97)
==6142==    by 0x4009F1: get_next_line (get_next_line.c:118)
==6142==    by 0x400A1C: main (get_next_line.c:128)


Note: see also the FAQ in the source distribution.
It contains workarounds to several common problems.
In particular, if Valgrind aborted or crashed after
identifying problems in your program, there's a good chance
that fixing those problems will prevent Valgrind aborting or
crashing, especially if it happened in m_mallocfree.c.

If that doesn't help, please report this bug to: www.valgrind.org

In the bug report, send all the above text, the valgrind
version, and what OS and version you are using.  Thanks.

看起来my_realloc函数中的free()导致程序崩溃,但我无法找到原因,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

重建realloc可能是一个好主意!

考虑您为start_at提供的realloc值。

更简单的重新分配方式:

char            *my_realloc(char *old, int size)
{
  int           i = 0;
  char          *new;

  new = malloc(sizeof(char) * (my_strlen(old) + size));
  while (old[i] != '\0')
    {
      new[i] = old[i];
      i++;
    }
  free(old);
  return (new);
}

另外,我更喜欢malloc这样:

malloc(sizeof(char) * len);

这个项目最好有一个get_char

char get_char(const int fd)
{
  static char buff[READ_MAX];
  static char* pointer;
  static int len = 0;
  char caracter;

  if (len == 0)
    {
      len = read(fd, buff, READ_MAX);
      pointer = (char*)&buff;
      if (len == 0)
        return (0);
    }
  caracter = *pointer;
  pointer++;
  len--;
  return caracter;
}
祝你好运!