LCOV - code coverage report
Current view: top level - src/common - string.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 15 17 88.2 %
Date: 2019-06-19 16:07:09 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * string.c
       4             :  *      string handling helpers
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/common/string.c
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : 
      17             : 
      18             : #ifndef FRONTEND
      19             : #include "postgres.h"
      20             : #else
      21             : #include "postgres_fe.h"
      22             : #endif
      23             : 
      24             : #include "common/string.h"
      25             : 
      26             : 
      27             : /*
      28             :  * Returns whether the string `str' has the postfix `end'.
      29             :  */
      30             : bool
      31        6450 : pg_str_endswith(const char *str, const char *end)
      32             : {
      33        6450 :     size_t      slen = strlen(str);
      34        6450 :     size_t      elen = strlen(end);
      35             : 
      36             :     /* can't be a postfix if longer */
      37        6450 :     if (elen > slen)
      38           0 :         return false;
      39             : 
      40             :     /* compare the end of the strings */
      41        6450 :     str += slen - elen;
      42        6450 :     return strcmp(str, end) == 0;
      43             : }
      44             : 
      45             : 
      46             : /*
      47             :  * strtoint --- just like strtol, but returns int not long
      48             :  */
      49             : int
      50      584582 : strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
      51             : {
      52             :     long        val;
      53             : 
      54      584582 :     val = strtol(str, endptr, base);
      55      584582 :     if (val != (int) val)
      56         462 :         errno = ERANGE;
      57      584582 :     return (int) val;
      58             : }
      59             : 
      60             : 
      61             : /*
      62             :  * pg_clean_ascii -- Replace any non-ASCII chars with a '?' char
      63             :  *
      64             :  * Modifies the string passed in which must be '\0'-terminated.
      65             :  *
      66             :  * This function exists specifically to deal with filtering out
      67             :  * non-ASCII characters in a few places where the client can provide an almost
      68             :  * arbitrary string (and it isn't checked to ensure it's a valid username or
      69             :  * database name or similar) and we don't want to have control characters or other
      70             :  * things ending up in the log file where server admins might end up with a
      71             :  * messed up terminal when looking at them.
      72             :  *
      73             :  * In general, this function should NOT be used- instead, consider how to handle
      74             :  * the string without needing to filter out the non-ASCII characters.
      75             :  *
      76             :  * Ultimately, we'd like to improve the situation to not require stripping out
      77             :  * all non-ASCII but perform more intelligent filtering which would allow UTF or
      78             :  * similar, but it's unclear exactly what we should allow, so stick to ASCII only
      79             :  * for now.
      80             :  */
      81             : void
      82       19034 : pg_clean_ascii(char *str)
      83             : {
      84             :     /* Only allow clean ASCII chars in the string */
      85             :     char       *p;
      86             : 
      87      275294 :     for (p = str; *p != '\0'; p++)
      88             :     {
      89      256260 :         if (*p < 32 || *p > 126)
      90           0 :             *p = '?';
      91             :     }
      92       19034 : }

Generated by: LCOV version 1.13