开发者

C regexec segfault

开发者 https://www.devze.com 2023-02-25 01:41 出处:网络
This is my first real attempt with C and regex so bear with me...I have a array of patterns (shortener_patterns) in which I pass to compile_patterns along with the count and array for compiled pattern

This is my first real attempt with C and regex so bear with me... I have a array of patterns (shortener_patterns) in which I pass to compile_patterns along with the count and array for compiled patterns (shortener_r_patterns). I am able to compile the patterns but the code will segfault on regexec. I guessing that it has something to do with how I am referencing shortener_r_patterns and my lack of fully understanding pointers. Or perhaps they are not getting compiled properly in the first place. Can someone get me going in the right direction?

char **shortener_patterns = '\0';

regex_t **shortener_r_patterns;

int *shortener_count = 0;


int compile_patterns(char **patterns, regex_t **r_patterns, int *count, int *compiled_count) {
    static int i;

    if (!(r_patterns = calloc(count, sizeof(regex_t)))) {
        return -1;
    }

    for (i=0; i<count; i++) {
        log(5, "compiling pattern %i: %s\n", i, patterns[i]);

        if (regcomp(&(r_patterns[i]), patterns[i], REG_EXTENDED|REG_NEWLINE|REG_ICASE|REG_NOSUB) != 0) {
            log(1, "invalid pattern: %s\n", patterns[i]);
            return -1;
        }
        // increment the compiled count
        (*compiled_count)++;
    }
    log(3, "finished compiling %i\n", count);

    return 0;
}

int check_shortener(char *domain) {
    static int i;

    if (!shortener_count) {
        return 0;
    }

    for (i=0; i<shortener_count; i++) {
        log(5, "checking shortner pattern '%s' against %s:%d\n", shortener_patterns[i], domain, i);
        if (regexec(&(shortener_r_patterns[i]), domain, 0, 0, 0) == 0) {
            log(1, "pattern %i '%s' found in %s\n", i, shortener_patterns[i], domain);
            return 1;
        }
    开发者_如何转开发}
    return 0;
}

Here is where I am scanning a file to get the contents for my patterns, adding to array and then finally compiling the array of patterns.

-- snip --

   while ((rc = fscanf(f, "%254[^\n]", buf)) > 0) {
        if (buf[0] == '#') {
            fscanf(f, "\n");
            continue;
        }
        newshortener = realloc(shortener_patterns, sizeof(char *) *(count+1));
        if (newshortener) {
            shortener_patterns = newshortener;
        }
        else {
            log(0, "could not realloc memory for shortener patterns\n");
            fclose(f);
            return -1;
        }
        shortener_patterns[count++] = strdup(buf);
        fscanf(f, "\n");
    }
    fclose(f);
    if (compile_patterns(shortener_patterns, shortener_r_patterns, count, &shortener_count) > 0) {
        log(0, "Failed to compile %s patterns\n", sfile);
    }

-- snip --


shortener_count does not have any space, you have defined it as an int * then set the pointer to 0, then you access the pointer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号