LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/krb5 - store_stdio.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 94 130 72.3 %
Date: 2024-02-29 22:57:05 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  *
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  *
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  *
      17             :  * 3. Neither the name of the Institute nor the names of its contributors
      18             :  *    may be used to endorse or promote products derived from this software
      19             :  *    without specific prior written permission.
      20             :  *
      21             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      22             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      25             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      26             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      27             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      28             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      29             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      30             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      31             :  * SUCH DAMAGE.
      32             :  */
      33             : 
      34             : #include "krb5_locl.h"
      35             : #include "store-int.h"
      36             : 
      37             : #ifndef HAVE_FSEEKO
      38             : #define fseeko fseek
      39             : #define ftello ftell
      40             : #endif
      41             : 
      42             : typedef struct stdio_storage {
      43             :     FILE *f;
      44             :     off_t pos;
      45             : } stdio_storage;
      46             : 
      47             : #define F(S) (((stdio_storage*)(S)->data)->f)
      48             : #define POS(S) (((stdio_storage*)(S)->data)->pos)
      49             : 
      50             : static ssize_t
      51     3444345 : stdio_fetch(krb5_storage * sp, void *data, size_t size)
      52             : {
      53     3444345 :     char *cbuf = (char *)data;
      54       52390 :     ssize_t count;
      55     3444345 :     size_t rem = size;
      56             : 
      57             :     /* similar pattern to net_read() to support pipes */
      58     6884032 :     while (rem > 0) {
      59     3444345 :         count = fread(cbuf, 1, rem, F(sp));
      60     3444345 :         if (count < 0) {
      61           0 :             POS(sp) = -1;
      62           0 :             if (errno == EINTR)
      63           0 :                 continue;
      64             :             else
      65           0 :                 return count;
      66     3444345 :         } else if (count == 0) {
      67        4658 :             if (POS(sp) >= 0)
      68        4658 :                 POS(sp) += size - rem;
      69        4658 :             return size - rem;
      70             :         }
      71     3439687 :         cbuf += count;
      72     3439687 :         rem -= count;
      73             :     }
      74     3439687 :     if (POS(sp) >= 0)
      75     3439687 :         POS(sp) += size;
      76     3439687 :     return size;
      77             : }
      78             : 
      79             : static ssize_t
      80        5808 : stdio_store(krb5_storage * sp, const void *data, size_t size)
      81             : {
      82        5808 :     const char *cbuf = (const char *)data;
      83         560 :     ssize_t count;
      84        5808 :     size_t rem = size;
      85             : 
      86             :     /*
      87             :      * It's possible we just went from reading to writing if the file was open
      88             :      * for both.  Per C99 (N869 final draft) section 7.18.5.3, point 6, when
      89             :      * going from reading to writing [a file opened for both] one must seek.
      90             :      */
      91        5808 :     (void) fseeko(F(sp), 0, SEEK_CUR);
      92             : 
      93             :     /* similar pattern to net_write() to support pipes */
      94       11616 :     while (rem > 0) {
      95        5808 :         count = fwrite(cbuf, 1, rem, F(sp));
      96        5808 :         if (count < 0) {
      97           0 :             if (errno == EINTR)
      98           0 :                 continue;
      99             :             /*
     100             :              * What does it mean to have a short write when using stdio?
     101             :              *
     102             :              * It can't mean much.  After all stdio is buffering, so
     103             :              * earlier writes that appeared complete may have failed,
     104             :              * and so we don't know how much we really failed to write.
     105             :              */
     106           0 :             POS(sp) = -1;
     107           0 :             return -1;
     108             :         }
     109        5808 :         if (count == 0) {
     110           0 :             POS(sp) = -1;
     111           0 :             return -1;
     112             :         }
     113        5808 :         cbuf += count;
     114        5808 :         rem -= count;
     115             :     }
     116        5808 :     if (POS(sp) >= 0)
     117        5808 :         POS(sp) += size;
     118        5808 :     return size;
     119             : }
     120             : 
     121             : static off_t
     122      651851 : stdio_seek(krb5_storage * sp, off_t offset, int whence)
     123             : {
     124      651851 :     int save_errno = errno;
     125             : 
     126      651851 :     if (whence == SEEK_SET && POS(sp) == offset)
     127      195860 :         return POS(sp);
     128             : 
     129      452826 :     if (whence == SEEK_CUR && POS(sp) >= 0 && offset == 0)
     130      425670 :         return POS(sp);
     131             : 
     132       19487 :     if (fseeko(F(sp), offset, whence) != 0)
     133           0 :         return -1;
     134       19487 :     errno = save_errno;
     135       19487 :     return POS(sp) = ftello(F(sp));
     136             : }
     137             : 
     138             : static int
     139        1754 : stdio_trunc(krb5_storage * sp, off_t offset)
     140             : {
     141         204 :     off_t tmpoff;
     142        1754 :     int save_errno = errno;
     143             : 
     144        1754 :     if (fflush(F(sp)) == EOF)
     145           0 :         return errno;
     146        1754 :     tmpoff = ftello(F(sp));
     147        1754 :     if (tmpoff < 0)
     148           0 :         return errno;
     149        1754 :     if (tmpoff > offset)
     150           0 :         tmpoff = offset;
     151        1754 :     if (ftruncate(fileno(F(sp)), offset) == -1)
     152           0 :         return errno;
     153        1754 :     if (fseeko(F(sp), 0, SEEK_END) == -1)
     154           0 :         return errno;
     155        1754 :     if (fseeko(F(sp), tmpoff, SEEK_SET) == -1)
     156           0 :         return errno;
     157        1754 :     errno = save_errno;
     158        1754 :     POS(sp) = tmpoff;
     159        1754 :     return 0;
     160             : }
     161             : 
     162             : static int
     163        2214 : stdio_sync(krb5_storage * sp)
     164             : {
     165        2214 :     if (fflush(F(sp)) == EOF)
     166           0 :         return errno;
     167        2214 :     if (fsync(fileno(F(sp))) == -1)
     168           0 :         return errno;
     169        1983 :     return 0;
     170             : }
     171             : 
     172             : static void
     173       62572 : stdio_free(krb5_storage * sp)
     174             : {
     175       62572 :     int save_errno = errno;
     176             : 
     177       62572 :     if (F(sp) != NULL && fclose(F(sp)) == 0)
     178       62572 :         errno = save_errno;
     179       62572 :     F(sp) = NULL;
     180       62572 : }
     181             : 
     182             : /**
     183             :  * Open a krb5_storage using stdio for buffering.
     184             :  *
     185             :  * @return A krb5_storage on success, or NULL on out of memory error.
     186             :  *
     187             :  * @ingroup krb5_storage
     188             :  *
     189             :  * @sa krb5_storage_emem()
     190             :  * @sa krb5_storage_from_fd()
     191             :  * @sa krb5_storage_from_mem()
     192             :  * @sa krb5_storage_from_readonly_mem()
     193             :  * @sa krb5_storage_from_data()
     194             :  * @sa krb5_storage_from_socket()
     195             :  */
     196             : 
     197             : KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL
     198       62572 : krb5_storage_stdio_from_fd(int fd_in, const char *mode)
     199             : {
     200        1170 :     krb5_storage *sp;
     201        1170 :     off_t off;
     202        1170 :     FILE *f;
     203       62572 :     int saved_errno = errno;
     204        1170 :     int fd;
     205             : 
     206       62572 :     off = lseek(fd_in, 0, SEEK_CUR);
     207       62572 :     if (off == -1)
     208           0 :         return NULL;
     209             : 
     210             : #ifdef _MSC_VER
     211             :     /*
     212             :      * This function used to try to pass the input to
     213             :      * _get_osfhandle() to test if the value is a HANDLE
     214             :      * but this doesn't work because doing so throws an
     215             :      * exception that will result in Watson being triggered
     216             :      * to file a Windows Error Report.
     217             :      */
     218             :     fd = _dup(fd_in);
     219             : #else
     220       62572 :     fd = dup(fd_in);
     221             : #endif
     222             : 
     223       62572 :     if (fd < 0)
     224           0 :         return NULL;
     225             : 
     226       62572 :     f = fdopen(fd, mode);
     227       62572 :     if (f == NULL) {
     228           0 :         (void) close(fd);
     229           0 :         return NULL;
     230             :     }
     231             : 
     232       62572 :     errno = saved_errno;
     233             : 
     234       62572 :     if (fseeko(f, off, SEEK_SET) == -1) {
     235           0 :         saved_errno = errno;
     236           0 :         (void) fclose(f);
     237           0 :         errno = saved_errno;
     238           0 :         return NULL;
     239             :     }
     240             : 
     241       62572 :     errno = ENOMEM;
     242       62572 :     sp = malloc(sizeof(krb5_storage));
     243       62572 :     if (sp == NULL) {
     244           0 :         saved_errno = errno;
     245           0 :         (void) fclose(f);
     246           0 :         errno = saved_errno;
     247           0 :         return NULL;
     248             :     }
     249             : 
     250       62572 :     errno = ENOMEM;
     251       62572 :     sp->data = malloc(sizeof(stdio_storage));
     252       62572 :     if (sp->data == NULL) {
     253           0 :         saved_errno = errno;
     254           0 :         (void) fclose(f);
     255           0 :         free(sp);
     256           0 :         errno = saved_errno;
     257           0 :         return NULL;
     258             :     }
     259       62572 :     sp->flags = 0;
     260       62572 :     sp->eof_code = HEIM_ERR_EOF;
     261       62572 :     F(sp) = f;
     262       62572 :     POS(sp) = off;
     263       62572 :     sp->fetch = stdio_fetch;
     264       62572 :     sp->store = stdio_store;
     265       62572 :     sp->seek = stdio_seek;
     266       62572 :     sp->trunc = stdio_trunc;
     267       62572 :     sp->fsync = stdio_sync;
     268       62572 :     sp->free = stdio_free;
     269       62572 :     sp->max_alloc = UINT32_MAX/64;
     270       62572 :     return sp;
     271             : }

Generated by: LCOV version 1.14