LCOV - code coverage report
Current view: top level - src/common - string.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 95.0 % 40 38
Test Date: 2026-11-22 16:16:07 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 83.3 % 24 20

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * string.c
       4                 :             :  *      string handling helpers
       5                 :             :  *
       6                 :             :  *
       7                 :             :  * Portions Copyright (c) 1996-2026, 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 <limits.h>
      25                 :             : 
      26                 :             : #include "common/string.h"
      27                 :             : 
      28                 :             : 
      29                 :             : /*
      30                 :             :  * Returns whether the string `str' has the postfix `end'.
      31                 :             :  */
      32                 :             : bool
      33                 :       20793 : pg_str_endswith(const char *str, const char *end)
      34                 :             : {
      35                 :       20793 :     size_t      slen = strlen(str);
      36                 :       20793 :     size_t      elen = strlen(end);
      37                 :             : 
      38                 :             :     /* can't be a postfix if longer */
      39         [ +  + ]:       20793 :     if (elen > slen)
      40                 :        2536 :         return false;
      41                 :             : 
      42                 :             :     /* compare the end of the strings */
      43                 :       18257 :     str += slen - elen;
      44                 :       18257 :     return strcmp(str, end) == 0;
      45                 :             : }
      46                 :             : 
      47                 :             : 
      48                 :             : /*
      49                 :             :  * strtoint --- just like strtol, but returns int not long
      50                 :             :  *
      51                 :             :  * The result is clamped to INT_MIN..INT_MAX, not LONG_MIN..LONG_MAX,
      52                 :             :  * but otherwise this has the same behavior as strtol.
      53                 :             :  */
      54                 :             : int
      55                 :      696926 : strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
      56                 :             : {
      57                 :             :     long        val;
      58                 :             : 
      59                 :      696926 :     val = strtol(str, endptr, base);
      60         [ +  + ]:      696926 :     if (val != (int) val)
      61                 :             :     {
      62         [ -  + ]:         922 :         val = (val < 0) ? INT_MIN : INT_MAX;
      63                 :         922 :         errno = ERANGE;
      64                 :             :     }
      65                 :      696926 :     return (int) val;
      66                 :             : }
      67                 :             : 
      68                 :             : 
      69                 :             : /*
      70                 :             :  * pg_clean_ascii -- Replace any non-ASCII chars with a "\xXX" string
      71                 :             :  *
      72                 :             :  * Makes a newly allocated copy of the string passed in, which must be
      73                 :             :  * '\0'-terminated. In the backend, additional alloc_flags may be provided and
      74                 :             :  * will be passed as-is to palloc_extended(); in the frontend, alloc_flags is
      75                 :             :  * ignored and the copy is malloc'd.
      76                 :             :  *
      77                 :             :  * This function exists specifically to deal with filtering out
      78                 :             :  * non-ASCII characters in a few places where the client can provide an almost
      79                 :             :  * arbitrary string (and it isn't checked to ensure it's a valid username or
      80                 :             :  * database name or similar) and we don't want to have control characters or other
      81                 :             :  * things ending up in the log file where server admins might end up with a
      82                 :             :  * messed up terminal when looking at them.
      83                 :             :  *
      84                 :             :  * In general, this function should NOT be used- instead, consider how to handle
      85                 :             :  * the string without needing to filter out the non-ASCII characters.
      86                 :             :  *
      87                 :             :  * Ultimately, we'd like to improve the situation to not require replacing all
      88                 :             :  * non-ASCII but perform more intelligent filtering which would allow UTF or
      89                 :             :  * similar, but it's unclear exactly what we should allow, so stick to ASCII only
      90                 :             :  * for now.
      91                 :             :  */
      92                 :             : char *
      93                 :       39172 : pg_clean_ascii(const char *str, int alloc_flags)
      94                 :             : {
      95                 :             :     size_t      dstlen;
      96                 :             :     char       *dst;
      97                 :             :     const char *p;
      98                 :       39172 :     size_t      i = 0;
      99                 :             : 
     100                 :             :     /* Worst case, each byte can become four bytes, plus a null terminator. */
     101                 :       39172 :     dstlen = strlen(str) * 4 + 1;
     102                 :             : 
     103                 :             : #ifdef FRONTEND
     104                 :           0 :     dst = malloc(dstlen);
     105                 :             : #else
     106                 :       39172 :     dst = palloc_extended(dstlen, alloc_flags);
     107                 :             : #endif
     108                 :             : 
     109         [ -  + ]:       39172 :     if (!dst)
     110                 :           0 :         return NULL;
     111                 :             : 
     112         [ +  + ]:      754993 :     for (p = str; *p != '\0'; p++)
     113                 :             :     {
     114                 :             : 
     115                 :             :         /* Only allow clean ASCII chars in the string */
     116   [ +  +  -  + ]:      715821 :         if (*p < 32 || *p > 126)
     117                 :             :         {
     118                 :             :             Assert(i < (dstlen - 3));
     119                 :          16 :             snprintf(&dst[i], dstlen - i, "\\x%02x", (unsigned char) *p);
     120                 :          16 :             i += 4;
     121                 :             :         }
     122                 :             :         else
     123                 :             :         {
     124                 :             :             Assert(i < dstlen);
     125                 :      715805 :             dst[i] = *p;
     126                 :      715805 :             i++;
     127                 :             :         }
     128                 :             :     }
     129                 :             : 
     130                 :             :     Assert(i < dstlen);
     131                 :       39172 :     dst[i] = '\0';
     132                 :       39172 :     return dst;
     133                 :             : }
     134                 :             : 
     135                 :             : 
     136                 :             : /*
     137                 :             :  * pg_is_ascii -- Check if string is made only of ASCII characters
     138                 :             :  */
     139                 :             : bool
     140                 :      100704 : pg_is_ascii(const char *str)
     141                 :             : {
     142         [ +  + ]:      485288 :     while (*str)
     143                 :             :     {
     144         [ +  + ]:      384586 :         if (IS_HIGHBIT_SET(*str))
     145                 :           2 :             return false;
     146                 :      384584 :         str++;
     147                 :             :     }
     148                 :      100702 :     return true;
     149                 :             : }
     150                 :             : 
     151                 :             : 
     152                 :             : /*
     153                 :             :  * pg_strip_crlf -- Remove any trailing newline and carriage return
     154                 :             :  *
     155                 :             :  * Removes any trailing newline and carriage return characters (\r on
     156                 :             :  * Windows) in the input string, zero-terminating it.
     157                 :             :  *
     158                 :             :  * The passed in string must be zero-terminated.  This function returns
     159                 :             :  * the new length of the string.
     160                 :             :  */
     161                 :             : int
     162                 :      235791 : pg_strip_crlf(char *str)
     163                 :             : {
     164                 :      235791 :     int         len = strlen(str);
     165                 :             : 
     166   [ +  +  +  + ]:      471582 :     while (len > 0 && (str[len - 1] == '\n' ||
     167         [ -  + ]:      227854 :                        str[len - 1] == '\r'))
     168                 :      235791 :         str[--len] = '\0';
     169                 :             : 
     170                 :      235791 :     return len;
     171                 :             : }
        

Generated by: LCOV version 2.0-1