lineread: Use ssize_t for line lengths
Functions and structures in lineread.c use plain int to record and report the length of lines we receive. This means we truncate the result from read(2) in some circumstances. Use ssize_t to avoid that. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
c919bbbdd3
commit
f9e8ee0777
3 changed files with 9 additions and 10 deletions
2
conf.c
2
conf.c
|
@ -401,9 +401,9 @@ static void get_dns(struct ctx *c)
|
||||||
struct fqdn *s = c->dns_search;
|
struct fqdn *s = c->dns_search;
|
||||||
struct lineread resolvconf;
|
struct lineread resolvconf;
|
||||||
unsigned int added = 0;
|
unsigned int added = 0;
|
||||||
|
ssize_t line_len;
|
||||||
char *line, *end;
|
char *line, *end;
|
||||||
const char *p;
|
const char *p;
|
||||||
int line_len;
|
|
||||||
|
|
||||||
dns4_set = !c->ifi4 || !IN4_IS_ADDR_UNSPECIFIED(dns4);
|
dns4_set = !c->ifi4 || !IN4_IS_ADDR_UNSPECIFIED(dns4);
|
||||||
dns6_set = !c->ifi6 || !IN6_IS_ADDR_UNSPECIFIED(dns6);
|
dns6_set = !c->ifi6 || !IN6_IS_ADDR_UNSPECIFIED(dns6);
|
||||||
|
|
10
lineread.c
10
lineread.c
|
@ -39,13 +39,11 @@ void lineread_init(struct lineread *lr, int fd)
|
||||||
*
|
*
|
||||||
* Return: length of line in bytes, -1 if no line was found
|
* Return: length of line in bytes, -1 if no line was found
|
||||||
*/
|
*/
|
||||||
static int peek_line(struct lineread *lr, bool eof)
|
static ssize_t peek_line(struct lineread *lr, bool eof)
|
||||||
{
|
{
|
||||||
char *nl;
|
char *nl;
|
||||||
|
|
||||||
/* Sanity checks (which also document invariants) */
|
/* Sanity checks (which also document invariants) */
|
||||||
ASSERT(lr->count >= 0);
|
|
||||||
ASSERT(lr->next_line >= 0);
|
|
||||||
ASSERT(lr->next_line + lr->count >= lr->next_line);
|
ASSERT(lr->next_line + lr->count >= lr->next_line);
|
||||||
ASSERT(lr->next_line + lr->count <= LINEREAD_BUFFER_SIZE);
|
ASSERT(lr->next_line + lr->count <= LINEREAD_BUFFER_SIZE);
|
||||||
|
|
||||||
|
@ -74,13 +72,13 @@ static int peek_line(struct lineread *lr, bool eof)
|
||||||
*
|
*
|
||||||
* Return: Length of line read on success, 0 on EOF, negative on error
|
* Return: Length of line read on success, 0 on EOF, negative on error
|
||||||
*/
|
*/
|
||||||
int lineread_get(struct lineread *lr, char **line)
|
ssize_t lineread_get(struct lineread *lr, char **line)
|
||||||
{
|
{
|
||||||
bool eof = false;
|
bool eof = false;
|
||||||
int line_len;
|
ssize_t line_len;
|
||||||
|
|
||||||
while ((line_len = peek_line(lr, eof)) < 0) {
|
while ((line_len = peek_line(lr, eof)) < 0) {
|
||||||
int rc;
|
ssize_t rc;
|
||||||
|
|
||||||
if ((lr->next_line + lr->count) == LINEREAD_BUFFER_SIZE) {
|
if ((lr->next_line + lr->count) == LINEREAD_BUFFER_SIZE) {
|
||||||
/* No space at end */
|
/* No space at end */
|
||||||
|
|
|
@ -18,14 +18,15 @@
|
||||||
* @buf: Buffer storing data read from file.
|
* @buf: Buffer storing data read from file.
|
||||||
*/
|
*/
|
||||||
struct lineread {
|
struct lineread {
|
||||||
int fd; int next_line;
|
int fd;
|
||||||
int count;
|
ssize_t next_line;
|
||||||
|
ssize_t count;
|
||||||
|
|
||||||
/* One extra byte for possible trailing \0 */
|
/* One extra byte for possible trailing \0 */
|
||||||
char buf[LINEREAD_BUFFER_SIZE+1];
|
char buf[LINEREAD_BUFFER_SIZE+1];
|
||||||
};
|
};
|
||||||
|
|
||||||
void lineread_init(struct lineread *lr, int fd);
|
void lineread_init(struct lineread *lr, int fd);
|
||||||
int lineread_get(struct lineread *lr, char **line);
|
ssize_t lineread_get(struct lineread *lr, char **line);
|
||||||
|
|
||||||
#endif /* _LINEREAD_H */
|
#endif /* _LINEREAD_H */
|
||||||
|
|
Loading…
Reference in a new issue