LCOV - code coverage report
Current view: top level - src/utils - strndup.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 8 9 88.9 %
Date: 2025-01-18 11:50:39 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2024  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This 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             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : 
      22             : #include <stdio.h>
      23             : 
      24             : #if HAVE_STDLIB_H
      25             : #include <stdlib.h>
      26             : #endif /* HAVE_STDLIB_H */
      27             : 
      28             : #if HAVE_STRING_H
      29             : #include <string.h>
      30             : #endif /* HAVE_STRING_H */
      31             : 
      32             : #include <freetds/macros.h>
      33             : #include <freetds/utils.h>
      34             : 
      35             : /**
      36             :  * Copy a string of length len to a new allocated buffer
      37             :  * This function does not read more than len bytes.
      38             :  * Please note that some system implementations of strndup
      39             :  * do not assure they don't read past len bytes as they
      40             :  * use still strlen to check length to copy limiting
      41             :  * after strlen to size passed.
      42             :  * String returned is NUL terminated.
      43             :  *
      44             :  * \param s   string to copy from
      45             :  * \param len length to copy
      46             :  *
      47             :  * \returns string copied or NULL if errors
      48             :  */
      49             : char *
      50        7272 : tds_strndup(const void *s, TDS_INTPTR len)
      51             : {
      52             :         char *out;
      53             :         const char *end;
      54             : 
      55        7272 :         if (len < 0)
      56             :                 return NULL;
      57             : 
      58        7272 :         end = (const char *) memchr(s, '\0', len);
      59        7272 :         if (end)
      60           0 :                 len = end - (const char *) s;
      61             : 
      62        7272 :         out = tds_new(char, len + 1);
      63        7272 :         if (out) {
      64        7272 :                 memcpy(out, s, len);
      65        7272 :                 out[len] = 0;
      66             :         }
      67             :         return out;
      68             : }
      69             : 

Generated by: LCOV version 1.13