繁体   English   中英

如果 getline 的 `size` 参数未重置为 0,则 Gnu 正则表达式会失败

[英]Gnu regex fails if `size` parameter of getline is not reset to 0

下面的代码由读取一堆文本文件的read_files()和使用 gnu regex 库对模式进行字符串匹配的match()函数组成。

read_files()我使用getline()并将size参数设置为 0 以便getline()将以默认的 120 大小开始,然后根据需要增加

#include <limits.h> // for PATH_MAX
#include <regex.h>  // for regcomp, regerror, regexec, regfree, size_t, REG...
#include <stdio.h>  // for printf, fprintf, NULL, fclose, fopen, getline
#include <stdlib.h> // for exit, free, EXIT_FAILURE

int match(const char *regex_str, const char *str) {

    regex_t regex;
    int reti;
    char msgbuf[100];

    /* Compile regular expression */
    reti = regcomp(&regex, regex_str, REG_EXTENDED);
    if (reti) {
        fprintf(stderr, "Could not compile regex\n");
        exit(1);
    }

    /* Execute regular expression */
    reti = regexec(&regex, str, 0, NULL, 0);
    if (!reti) {
        return 1;
    } else if (reti == REG_NOMATCH) {
        return 0;
    } else {
        regerror(reti, &regex, msgbuf, sizeof(msgbuf));
        fprintf(stderr, "Regex match failed: %s\n", msgbuf);
        exit(1);
    }

    /* Free memory allocated to the pattern buffer by regcomp() */
    regfree(&regex);
}

void read_files() {

    size_t path_count = 2;
    char pathnames[2][PATH_MAX] = {"./tmp/test0.conf", "./tmp/test1.conf"};

    FILE *fp;
    char *line = NULL;
    size_t len = 0;
    ssize_t read_count;

    for (int i = 0; i < path_count; i++) {
        printf("opening file %s\n", pathnames[i]);

        fp = fopen(pathnames[i], "r");
        if (fp == NULL) {
            printf("internal error,couldn't open file %s\"}", pathnames[i]);
            exit(EXIT_FAILURE);
        }
        int linenum=1;
        while ((read_count = getline(&line, &len, fp)) != -1) {
            printf("%d: %s",linenum,line);
            linenum++;
        }
        printf("len: %zu\n", len);

        fclose(fp);
        // len=0; // this is the line that fixes the bug, if i reset len to 0 after reading the first file then everything works as expected, if i don't reset it then regex matching fails
        if (line)
            free(line);
    }
}

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

    if (!match("^[a-zA-Z0-9]+$", "jack")) {
        printf("input don't match\n");
    }
}

test0.conf 的内容

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

test1.conf 的内容

testing123

运行上述代码时,我得到以下输出:

opening file ./tmp/test0.conf
1: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
len: 240
opening file ./tmp/test1.conf
1: testing123
len: 240
input don't match

所以模式匹配失败,字符串“jack”实际上匹配。

您可以看到,在完成读取第一个文件后, len设置为 240,因此当getline再次为第二个文件执行时,它将读取缓冲区大小为240的文件,但这由于某种原因导致正则表达式匹配失败。

如果我在读取第一个文件后将len重置为 0 参数,则代码按预期工作(正则表达式匹配工作正常)。

那么为什么getline() len参数会影响 gnu 正则表达式的行为呢?

那么为什么 getline() len 参数会影响 gnu 正则表达式的行为呢?

正如玛丽安评论的那样,您使用getline不正确,导致它损坏堆。 您可以通过使用-fsanitize=address标志编译程序并运行它来观察这一点。 请参阅Address Sanitizer 手册以了解错误。

这是未定义的行为,您的程序可以做任何事情 在这里,它恰好导致 GNU 正则表达式库停止正常工作。 SIGSEGV是另一个可能的结果。

要解决此问题,您应该将free调用移出循环,并且仅在完成读取行后才释放内存。

free后在循环中设置line = NULL是另一种可能(但效率较低)的修复方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM