Coverage Report

Created: 2025-11-11 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curl_get_line.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) ||  \
28
  !defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC)
29
30
#include "curl_get_line.h"
31
#include "curl_memory.h"
32
/* The last #include file should be: */
33
#include "memdebug.h"
34
35
#define appendnl(b)                             \
36
401k
  curlx_dyn_addn(buf, "\n", 1)
37
38
/*
39
 * Curl_get_line() returns only complete whole lines that end with newline.
40
 * When 'eof' is set TRUE, the last line has been read.
41
 */
42
CURLcode Curl_get_line(struct dynbuf *buf, FILE *input, bool *eof)
43
401k
{
44
401k
  CURLcode result;
45
401k
  char buffer[128];
46
401k
  curlx_dyn_reset(buf);
47
401k
  while(1) {
48
401k
    size_t rlen;
49
401k
    char *b = fgets(buffer, sizeof(buffer), input);
50
51
401k
    *eof = feof(input);
52
53
401k
    rlen = b ? strlen(b) : 0;
54
401k
    if(rlen) {
55
0
      result = curlx_dyn_addn(buf, b, rlen);
56
0
      if(result)
57
        /* too long line or out of memory */
58
0
        return result;
59
0
    }
60
    /* now check the full line */
61
401k
    rlen = curlx_dyn_len(buf);
62
401k
    b = curlx_dyn_ptr(buf);
63
401k
    if(rlen && (b[rlen-1] == '\n'))
64
      /* LF at end of the line */
65
0
      return CURLE_OK; /* all good */
66
401k
    if(*eof)
67
      /* append a newline */
68
401k
      return appendnl(buf);
69
    /* otherwise get next line to append */
70
401k
  }
71
  /* UNREACHABLE */
72
401k
}
73
74
#endif /* if not disabled */