LCOV - code coverage report
Current view: top level - src/backend/tsearch - ts_utils.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 40 45 88.9 %
Date: 2019-11-21 13:06:38 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * ts_utils.c
       4             :  *      various support functions
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  *
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/tsearch/ts_utils.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #include "postgres.h"
      16             : 
      17             : #include <ctype.h>
      18             : 
      19             : #include "miscadmin.h"
      20             : #include "tsearch/ts_locale.h"
      21             : #include "tsearch/ts_utils.h"
      22             : 
      23             : 
      24             : /*
      25             :  * Given the base name and extension of a tsearch config file, return
      26             :  * its full path name.  The base name is assumed to be user-supplied,
      27             :  * and is checked to prevent pathname attacks.  The extension is assumed
      28             :  * to be safe.
      29             :  *
      30             :  * The result is a palloc'd string.
      31             :  */
      32             : char *
      33         262 : get_tsearch_config_filename(const char *basename,
      34             :                             const char *extension)
      35             : {
      36             :     char        sharepath[MAXPGPATH];
      37             :     char       *result;
      38             : 
      39             :     /*
      40             :      * We limit the basename to contain a-z, 0-9, and underscores.  This may
      41             :      * be overly restrictive, but we don't want to allow access to anything
      42             :      * outside the tsearch_data directory, so for instance '/' *must* be
      43             :      * rejected, and on some platforms '\' and ':' are risky as well. Allowing
      44             :      * uppercase might result in incompatible behavior between case-sensitive
      45             :      * and case-insensitive filesystems, and non-ASCII characters create other
      46             :      * interesting risks, so on the whole a tight policy seems best.
      47             :      */
      48         262 :     if (strspn(basename, "abcdefghijklmnopqrstuvwxyz0123456789_") != strlen(basename))
      49           0 :         ereport(ERROR,
      50             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
      51             :                  errmsg("invalid text search configuration file name \"%s\"",
      52             :                         basename)));
      53             : 
      54         262 :     get_share_path(my_exec_path, sharepath);
      55         262 :     result = palloc(MAXPGPATH);
      56         262 :     snprintf(result, MAXPGPATH, "%s/tsearch_data/%s.%s",
      57             :              sharepath, basename, extension);
      58             : 
      59         262 :     return result;
      60             : }
      61             : 
      62             : /*
      63             :  * Reads a stop-word file. Each word is run through 'wordop'
      64             :  * function, if given.  wordop may either modify the input in-place,
      65             :  * or palloc a new version.
      66             :  */
      67             : void
      68          26 : readstoplist(const char *fname, StopList *s, char *(*wordop) (const char *))
      69             : {
      70          26 :     char      **stop = NULL;
      71             : 
      72          26 :     s->len = 0;
      73          26 :     if (fname && *fname)
      74             :     {
      75          26 :         char       *filename = get_tsearch_config_filename(fname, "stop");
      76             :         tsearch_readline_state trst;
      77             :         char       *line;
      78          26 :         int         reallen = 0;
      79             : 
      80          26 :         if (!tsearch_readline_begin(&trst, filename))
      81           0 :             ereport(ERROR,
      82             :                     (errcode(ERRCODE_CONFIG_FILE_ERROR),
      83             :                      errmsg("could not open stop-word file \"%s\": %m",
      84             :                             filename)));
      85             : 
      86        3354 :         while ((line = tsearch_readline(&trst)) != NULL)
      87             :         {
      88        3302 :             char       *pbuf = line;
      89             : 
      90             :             /* Trim trailing space */
      91       19474 :             while (*pbuf && !t_isspace(pbuf))
      92       12870 :                 pbuf += pg_mblen(pbuf);
      93        3302 :             *pbuf = '\0';
      94             : 
      95             :             /* Skip empty lines */
      96        3302 :             if (*line == '\0')
      97             :             {
      98           0 :                 pfree(line);
      99           0 :                 continue;
     100             :             }
     101             : 
     102        3302 :             if (s->len >= reallen)
     103             :             {
     104          52 :                 if (reallen == 0)
     105             :                 {
     106          26 :                     reallen = 64;
     107          26 :                     stop = (char **) palloc(sizeof(char *) * reallen);
     108             :                 }
     109             :                 else
     110             :                 {
     111          26 :                     reallen *= 2;
     112          26 :                     stop = (char **) repalloc((void *) stop,
     113             :                                               sizeof(char *) * reallen);
     114             :                 }
     115             :             }
     116             : 
     117        3302 :             if (wordop)
     118             :             {
     119        3302 :                 stop[s->len] = wordop(line);
     120        3302 :                 if (stop[s->len] != line)
     121        3302 :                     pfree(line);
     122             :             }
     123             :             else
     124           0 :                 stop[s->len] = line;
     125             : 
     126        3302 :             (s->len)++;
     127             :         }
     128             : 
     129          26 :         tsearch_readline_end(&trst);
     130          26 :         pfree(filename);
     131             :     }
     132             : 
     133          26 :     s->stop = stop;
     134             : 
     135             :     /* Sort to allow binary searching */
     136          26 :     if (s->stop && s->len > 0)
     137          26 :         qsort(s->stop, s->len, sizeof(char *), pg_qsort_strcmp);
     138          26 : }
     139             : 
     140             : bool
     141        8698 : searchstoplist(StopList *s, char *key)
     142             : {
     143       14306 :     return (s->stop && s->len > 0 &&
     144        5608 :             bsearch(&key, s->stop, s->len,
     145       14306 :                     sizeof(char *), pg_qsort_strcmp)) ? true : false;
     146             : }

Generated by: LCOV version 1.13