(root)/
glibc-2.38/
nss/
rewrite_field.c
       1  /* Copyright (C) 2015-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <nss.h>
      19  #include <string.h>
      20  
      21  /* Rewrite VALUE to a valid field value in the NSS database.  Invalid
      22     characters are replaced with a single space character ' '.  If
      23     VALUE is NULL, the empty string is returned.  *TO_BE_FREED is
      24     overwritten with a pointer the caller has to free if the function
      25     returns successfully.  On failure, return NULL.  */
      26  const char *
      27  __nss_rewrite_field (const char *value, char **to_be_freed)
      28  {
      29    *to_be_freed = NULL;
      30    if (value == NULL)
      31      return "";
      32  
      33    /* Search for non-allowed characters.  */
      34    const char *p = strpbrk (value, __nss_invalid_field_characters);
      35    if (p == NULL)
      36      return value;
      37    *to_be_freed = __strdup (value);
      38    if (*to_be_freed == NULL)
      39      return NULL;
      40  
      41    /* Switch pointer to freshly-allocated buffer.  */
      42    char *bad = *to_be_freed + (p - value);
      43    do
      44      {
      45        *bad = ' ';
      46        bad = strpbrk (bad + 1, __nss_invalid_field_characters);
      47      }
      48    while (bad != NULL);
      49  
      50    return *to_be_freed;
      51  }