title: C regexec segfault
body: <p>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 <code>compile_patterns</code> 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 <code>regexec</code>.  I guessing that it has something to do with how I am referencing <code>shortener_r_patterns</code> 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?</p>
  
  <pre><code>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&lt;count; i++) {
          log(5, "compiling pattern %i: %s\n", i, patterns[i]);
  
          if (regcomp(&amp;(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&lt;shortener_count; i++) {
          log(5, "checking shortner pattern '%s' against %s:%d\n", shortener_patterns[i], domain, i);
          if (regexec(&amp;(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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>-- snip --</p>
  
  <pre><code>   while ((rc = fscanf(f, "%254[^\n]", buf)) &gt; 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, &amp;shortener_count) &gt; 0) {
          log(0, "Failed to compile %s patterns\n", sfile);
      }
  </code></pre>
  
  <p>-- snip --</p>