Skip to content

Commit 610b03e

Browse files
committed
Fix potential insertion of nul byte into notifier logs (Issue #1450)
1 parent 1388018 commit 610b03e

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

scheduler/statbuf.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
22
* Status buffer routines for the CUPS scheduler.
33
*
4-
* Copyright © 2020-2024 by OpenPrinting.
5-
* Copyright 2007-2014 by Apple Inc.
6-
* Copyright 1997-2006 by Easy Software Products, all rights reserved.
4+
* Copyright © 2020-2026 by OpenPrinting.
5+
* Copyright © 2007-2014 by Apple Inc.
6+
* Copyright © 1997-2006 by Easy Software Products, all rights reserved.
77
*
8-
* Licensed under Apache License v2.0. See the file "LICENSE" for more information.
8+
* Licensed under Apache License v2.0. See the file "LICENSE" for more
9+
* information.
910
*/
1011

1112
/*
@@ -127,7 +128,10 @@ cupsdStatBufUpdate(
127128
* No, read more data...
128129
*/
129130

130-
if ((bytes = read(sb->fd, sb->buffer + sb->bufused, (size_t)(CUPSD_SB_BUFFER_SIZE - sb->bufused - 1))) > 0)
131+
if ((bytes = CUPSD_SB_BUFFER_SIZE - 1 - sb->bufused) > 0)
132+
bytes = read(sb->fd, sb->buffer + sb->bufused, (size_t)bytes);
133+
134+
if (bytes > 0)
131135
{
132136
sb->bufused += bytes;
133137
sb->buffer[sb->bufused] = '\0';
@@ -136,8 +140,7 @@ cupsdStatBufUpdate(
136140
* Guard against a line longer than the max buffer size...
137141
*/
138142

139-
if ((lineptr = strchr(sb->buffer, '\n')) == NULL &&
140-
sb->bufused == (CUPSD_SB_BUFFER_SIZE - 1))
143+
if ((lineptr = strchr(sb->buffer, '\n')) == NULL && sb->bufused == (CUPSD_SB_BUFFER_SIZE - 1))
141144
lineptr = sb->buffer + sb->bufused;
142145
}
143146
else if (bytes < 0 && errno == EINTR)
@@ -157,8 +160,7 @@ cupsdStatBufUpdate(
157160
* End-of-file, so use the whole buffer...
158161
*/
159162

160-
lineptr = sb->buffer + sb->bufused;
161-
*lineptr = '\0';
163+
lineptr = sb->buffer + sb->bufused;
162164
}
163165

164166
/*
@@ -185,7 +187,8 @@ cupsdStatBufUpdate(
185187
* Terminate the line and process it...
186188
*/
187189

188-
*lineptr++ = '\0';
190+
if (*lineptr)
191+
*lineptr++ = '\0';
189192

190193
/*
191194
* Figure out the logging level...
@@ -280,8 +283,7 @@ cupsdStatBufUpdate(
280283

281284
if (sb->prefix[0])
282285
{
283-
if (*loglevel > CUPSD_LOG_NONE &&
284-
(*loglevel != CUPSD_LOG_INFO || LogLevel >= CUPSD_LOG_DEBUG))
286+
if (*loglevel > CUPSD_LOG_NONE && (*loglevel != CUPSD_LOG_INFO || LogLevel >= CUPSD_LOG_DEBUG))
285287
{
286288
/*
287289
* General status message; send it to the error_log file...
@@ -293,7 +295,9 @@ cupsdStatBufUpdate(
293295
cupsdLogMessage(*loglevel, "%s %s", sb->prefix, message);
294296
}
295297
else if (*loglevel < CUPSD_LOG_NONE && LogLevel >= CUPSD_LOG_DEBUG)
298+
{
296299
cupsdLogMessage(CUPSD_LOG_DEBUG2, "%s %s", sb->prefix, sb->buffer);
300+
}
297301
}
298302

299303
/*

0 commit comments

Comments
 (0)