LCOV - code coverage report
Current view: top level - source3/lib - substitute_generic.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 35 43 81.4 %
Date: 2024-04-13 12:30:31 Functions: 2 2 100.0 %

          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             : /**
      28             :  Similar to string_sub2, but it will accept only allocated strings
      29             :  and may realloc them so pay attention at what you pass on no
      30             :  pointers inside strings, no const may be passed
      31             :  as string.
      32             : **/
      33             : 
      34      552644 : char *realloc_string_sub2(char *string,
      35             :                         const char *pattern,
      36             :                         const char *insert,
      37             :                         bool remove_unsafe_characters,
      38             :                         bool allow_trailing_dollar)
      39             : {
      40        3987 :         char *p, *in;
      41        3987 :         char *s;
      42        3987 :         ssize_t ls,lp,li,ld, i;
      43             : 
      44      552644 :         if (!insert || !pattern || !*pattern || !string || !*string)
      45           0 :                 return NULL;
      46             : 
      47      552644 :         s = string;
      48             : 
      49      552644 :         in = talloc_strdup(talloc_tos(), insert);
      50      552644 :         if (!in) {
      51           0 :                 DEBUG(0, ("realloc_string_sub: out of memory!\n"));
      52           0 :                 return NULL;
      53             :         }
      54      552644 :         ls = (ssize_t)strlen(s);
      55      552644 :         lp = (ssize_t)strlen(pattern);
      56      552644 :         li = (ssize_t)strlen(insert);
      57      552644 :         ld = li - lp;
      58     4545747 :         for (i=0;i<li;i++) {
      59     3993103 :                 switch (in[i]) {
      60        4277 :                         case '$':
      61             :                                 /* allow a trailing $
      62             :                                  * (as in machine accounts) */
      63        4277 :                                 if (allow_trailing_dollar && (i == li - 1 )) {
      64           0 :                                         break;
      65             :                                 }
      66           0 :                                 FALL_THROUGH;
      67             :                         case '`':
      68             :                         case '"':
      69             :                         case '\'':
      70             :                         case ';':
      71             :                         case '%':
      72             :                         case '\r':
      73             :                         case '\n':
      74        4277 :                                 if ( remove_unsafe_characters ) {
      75        4277 :                                         in[i] = '_';
      76        4277 :                                         break;
      77             :                                 }
      78             :                                 FALL_THROUGH;
      79             :                         default:
      80             :                                 /* ok */
      81     3945667 :                                 break;
      82             :                 }
      83             :         }
      84             : 
      85     1105288 :         while ((p = strstr_m(s,pattern))) {
      86      552644 :                 if (ld > 0) {
      87      461214 :                         int offset = PTR_DIFF(s,string);
      88      461214 :                         string = talloc_realloc(NULL, string, char, ls + ld + 1);
      89      461214 :                         if (!string) {
      90           0 :                                 DEBUG(0, ("realloc_string_sub: "
      91             :                                         "out of memory!\n"));
      92           0 :                                 talloc_free(in);
      93           0 :                                 return NULL;
      94             :                         }
      95      461214 :                         p = string + offset + (p - s);
      96             :                 }
      97      552644 :                 if (li != lp) {
      98      552644 :                         memmove(p+li,p+lp,strlen(p+lp)+1);
      99             :                 }
     100      552644 :                 memcpy(p, in, li);
     101      552644 :                 s = p + li;
     102      552644 :                 ls += ld;
     103             :         }
     104      552644 :         talloc_free(in);
     105      552644 :         return string;
     106             : }
     107             : 
     108      552644 : char *realloc_string_sub(char *string,
     109             :                         const char *pattern,
     110             :                         const char *insert)
     111             : {
     112      552644 :         return realloc_string_sub2(string, pattern, insert, true, false);
     113             : }

Generated by: LCOV version 1.14