LCOV - code coverage report
Current view: top level - source3/lib - substitute_generic.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 32 43 74.4 %
Date: 2021-09-23 10:06:22 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Samba utility functions
       4             : 
       5             :    Copyright (C) Andrew Tridgell 1992-2001
       6             :    Copyright (C) Simo Sorce      2001-2002
       7             :    Copyright (C) Martin Pool     2003
       8             :    Copyright (C) James Peach     2006
       9             :    Copyright (C) Jeremy Allison  1992-2007
      10             : 
      11             :    This program is free software; you can redistribute it and/or modify
      12             :    it under the terms of the GNU General Public License as published by
      13             :    the Free Software Foundation; either version 3 of the License, or
      14             :    (at your option) any later version.
      15             : 
      16             :    This program is distributed in the hope that it will be useful,
      17             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :    GNU General Public License for more details.
      20             : 
      21             :    You should have received a copy of the GNU General Public License
      22             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include "includes.h"
      26             : 
      27           0 : void fstring_sub(char *s,const char *pattern,const char *insert)
      28             : {
      29           0 :         string_sub(s, pattern, insert, sizeof(fstring));
      30           0 : }
      31             : 
      32             : /**
      33             :  Similar to string_sub2, but it will accept only allocated strings
      34             :  and may realloc them so pay attention at what you pass on no
      35             :  pointers inside strings, no const may be passed
      36             :  as string.
      37             : **/
      38             : 
      39      236960 : char *realloc_string_sub2(char *string,
      40             :                         const char *pattern,
      41             :                         const char *insert,
      42             :                         bool remove_unsafe_characters,
      43             :                         bool allow_trailing_dollar)
      44             : {
      45             :         char *p, *in;
      46             :         char *s;
      47             :         ssize_t ls,lp,li,ld, i;
      48             : 
      49      236960 :         if (!insert || !pattern || !*pattern || !string || !*string)
      50           0 :                 return NULL;
      51             : 
      52      236960 :         s = string;
      53             : 
      54      236960 :         in = talloc_strdup(talloc_tos(), insert);
      55      236960 :         if (!in) {
      56           0 :                 DEBUG(0, ("realloc_string_sub: out of memory!\n"));
      57           0 :                 return NULL;
      58             :         }
      59      236960 :         ls = (ssize_t)strlen(s);
      60      236960 :         lp = (ssize_t)strlen(pattern);
      61      236960 :         li = (ssize_t)strlen(insert);
      62      236960 :         ld = li - lp;
      63     2362142 :         for (i=0;i<li;i++) {
      64     2125182 :                 switch (in[i]) {
      65        2222 :                         case '$':
      66             :                                 /* allow a trailing $
      67             :                                  * (as in machine accounts) */
      68        2222 :                                 if (allow_trailing_dollar && (i == li - 1 )) {
      69           0 :                                         break;
      70             :                                 }
      71             :                                 FALL_THROUGH;
      72             :                         case '`':
      73             :                         case '"':
      74             :                         case '\'':
      75             :                         case ';':
      76             :                         case '%':
      77             :                         case '\r':
      78             :                         case '\n':
      79        2222 :                                 if ( remove_unsafe_characters ) {
      80        2222 :                                         in[i] = '_';
      81        2222 :                                         break;
      82             :                                 }
      83             :                                 FALL_THROUGH;
      84             :                         default:
      85             :                                 /* ok */
      86     2089908 :                                 break;
      87             :                 }
      88           0 :         }
      89             : 
      90      689965 :         while ((p = strstr_m(s,pattern))) {
      91      236960 :                 if (ld > 0) {
      92      233577 :                         int offset = PTR_DIFF(s,string);
      93      233577 :                         string = talloc_realloc(NULL, string, char, ls + ld + 1);
      94      233577 :                         if (!string) {
      95           0 :                                 DEBUG(0, ("realloc_string_sub: "
      96             :                                         "out of memory!\n"));
      97           0 :                                 talloc_free(in);
      98           0 :                                 return NULL;
      99             :                         }
     100      233577 :                         p = string + offset + (p - s);
     101             :                 }
     102      236960 :                 if (li != lp) {
     103      236960 :                         memmove(p+li,p+lp,strlen(p+lp)+1);
     104             :                 }
     105      240042 :                 memcpy(p, in, li);
     106      236960 :                 s = p + li;
     107      236960 :                 ls += ld;
     108             :         }
     109      236960 :         talloc_free(in);
     110      236960 :         return string;
     111             : }
     112             : 
     113      236960 : char *realloc_string_sub(char *string,
     114             :                         const char *pattern,
     115             :                         const char *insert)
     116             : {
     117      236960 :         return realloc_string_sub2(string, pattern, insert, true, false);
     118             : }

Generated by: LCOV version 1.13