Bug Summary

File:_build/../src/slowcat.c
Warning:line 60, column 8
File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O3 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name slowcat.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/rootdir/_build -fcoverage-compilation-dir=/rootdir/_build -resource-dir /usr/lib/llvm-21/lib/clang/21 -I src/slowcat.p -I src -I ../src -I . -I .. -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/15/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-address-of-packed-member -Wno-missing-field-initializers -Wno-packed -Wno-switch-enum -Wno-unused-parameter -Wwrite-strings -std=gnu11 -fconst-strings -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-checker deadcode.DeadStores -analyzer-checker security.ArrayBound -analyzer-checker unix.cstring.NotNullTerminated -analyzer-checker alpha.deadcode.UnreachableCode -analyzer-checker alpha.core.CastToStruct -analyzer-checker alpha.security.ReturnPtrRange -analyzer-checker alpha.unix.SimpleStream -analyzer-checker alpha.unix.cstring.BufferOverlap -analyzer-checker alpha.unix.cstring.OutOfBounds -analyzer-checker alpha.core.FixedAddr -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /rootdir/html-report/2026-01-19-185356-10936-1 -x c ../src/slowcat.c
1/*
2 * Copyright (C) 2002 Red Hat, Inc.
3 *
4 * This 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 * 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 * 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 this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <config.h>
20#include <sys/time.h>
21#include <sys/types.h>
22#include <errno(*__errno_location ()).h>
23#include <signal.h>
24#include <stdbool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29#include <glib.h>
30
31static void
32catfile(const char *pathname, long delay, long chunksize)
33{
34 FILE *fp;
35 struct timeval tv;
36 char *buf;
37 int c;
38 long i;
39 bool_Bool is_opened = FALSE(0);
40
41 if (!((pathname == NULL((void*)0)) || (strcmp(pathname, "-") == 0))) {
11
Assuming 'pathname' is not equal to NULL
12
Assuming the condition is false
13
Taking true branch
42 fp = fopen(pathname, "r");
43 is_opened = TRUE(!(0));
44 if (fp
13.1
'fp' is not equal to NULL
== NULL((void*)0)) {
14
Taking false branch
45 g_warning("Error opening file `%s': %s.\n",
46 pathname, strerror(errno(*__errno_location ())));
47 return;
48 }
49 } else {
50 fp = stdinstdin;
51 }
52
53 buf = g_malloc(chunksize);
54
55 while (!feof(fp)) {
15
Loop condition is true. Entering loop body
21
Loop condition is true. Entering loop body
56 tv.tv_sec = delay / 1000000;
57 tv.tv_usec = delay % 1000000;
58 select(0, NULL((void*)0), NULL((void*)0), NULL((void*)0), &tv);
59 for (i = 0; i < chunksize; i++) {
16
Loop condition is true. Entering loop body
22
Loop condition is true. Entering loop body
60 c = fgetc(fp);
17
Assuming this stream operation fails
23
File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior
61 if (c != EOF(-1)) {
18
Taking false branch
62 buf[i] = c;
63 } else {
64 break;
65 }
66 }
67 if (i
19.1
'i' is <= 0
> 0) {
19
Execution continues on line 67
20
Taking false branch
68 int bytes G_GNUC_UNUSED__attribute__ ((__unused__));
69 bytes = write(STDOUT_FILENO1, buf, i);
70 fsync(STDOUT_FILENO1);
71 }
72 }
73
74 g_free(buf);
75
76 if (is_opened) {
77 fclose(fp);
78 }
79}
80
81int
82main(int argc, char **argv)
83{
84 int i, c;
85 long delay = 200000, chunksize = 1, tmp;
86 char *p;
87 GList *files = NULL((void*)0);
88
89 while ((c = getopt(argc, argv, "t:c:")) != -1) {
1
Assuming the condition is false
2
Loop condition is false. Execution continues on line 109
90 switch (c) {
91 case 't':
92 tmp = strtol(optarg, &p, 0);
93 if ((p != NULL((void*)0)) && (*p == '\0')) {
94 delay = tmp;
95 }
96 break;
97 case 'c':
98 tmp = strtol(optarg, &p, 0);
99 if ((p != NULL((void*)0)) && (*p == '\0')) {
100 chunksize = tmp;
101 }
102 break;
103 default:
104 g_printerr("Usage: slowcat [-t delay] [-c chunksize] [file ...]\n");
105 exit(1);
106 break;
107 }
108 }
109 for (i = optind; i < argc; i++) {
3
Assuming 'i' is < 'argc'
4
Loop condition is true. Entering loop body
5
Assuming 'i' is >= 'argc'
6
Loop condition is false. Execution continues on line 113
110 files = g_list_append(files, argv[i]);
111 }
112
113 if (files) {
7
Assuming 'files' is non-null
8
Taking true branch
114 GList *file;
115 for (file = files; file
8.1
'file' is not equal to NULL
!= NULL((void*)0); file = g_list_next(file)((file) ? (((GList *)(file))->next) : ((void*)0))) {
9
Loop condition is true. Entering loop body
116 catfile((const char*)file->data, delay, chunksize);
10
Calling 'catfile'
117 }
118 } else {
119 catfile(NULL((void*)0), delay, chunksize);
120 }
121 return 0;
122}