Skip to contentSkip to conversation

samples: bpf: fix: change the buffer size for read()

PATCH Applied Chang-Hsien Tsai 3 participants 4 messages May 19, 2019 – May 22, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 samples: bpf: fix: change the buffer size for read()

If the trace for read is larger than 4096,
the return value sz will be 4096.
This results in off-by-one error on buf.

    static char buf[4096];
    ssize_t sz;

    sz = read(trace_fd, buf, sizeof(buf));
    if (sz > 0) {
        buf[sz] = 0;
        puts(buf);
    }
---
 samples/bpf/bpf_load.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/bpf/bpf_load.c +1 -1
index eae7b635343d..d4da90070b58 100644
@@ -678,7 +678,7 @@ void read_trace_pipe(void)
 		static char buf[4096];
 		ssize_t sz;
 
-		sz = read(trace_fd, buf, sizeof(buf));
+		sz = read(trace_fd, buf, sizeof(buf)-1);
 		if (sz > 0) {
 			buf[sz] = 0;
 			puts(buf);
Signature · 1 line
2.17.1
maintainer Applied
#1 lore raw
Quoted 13 lines · Chang-Hsien Tsai

If the trace for read is larger than 4096,
the return value sz will be 4096.
This results in off-by-one error on buf.

    static char buf[4096];
    ssize_t sz;

    sz = read(trace_fd, buf, sizeof(buf));
    if (sz > 0) {
        buf[sz] = 0;
        puts(buf);
    }
Quoted 1 line

Applied, thanks!

#2 lore raw
In reply to Chang-Hsien Tsai

From: Chang-Hsien Tsai

Quoted 24 lines

Sent: 19 May 2019 10:06
If the trace for read is larger than 4096, the return value sz will be 4096. This results in off-by-one error on buf.

    static char buf[4096];
    ssize_t sz;

    sz = read(trace_fd, buf, sizeof(buf));
    if (sz > 0) {
        buf[sz] = 0;
        puts(buf);
    }
---
 samples/bpf/bpf_load.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/bpf/bpf_load.c +0 -0
index eae7b635343d..d4da90070b58 100644

samples/bpf/bpf_load.c void read_trace_pipe(void) · line 684

Quoted 8 lines
samples/bpf/bpf_load.c +1 -1
@@ -678,7 +678,7 @@ void read_trace_pipe(void)
 		static char buf[4096];
 		ssize_t sz;
 
-		sz = read(trace_fd, buf, sizeof(buf));
+		sz = read(trace_fd, buf, sizeof(buf)-1);
 		if (sz > 0) {
 			buf[sz] = 0;
 			puts(buf);

Why not change the puts() to fwrite(buf, sz, 1, stdout) ? Then you don't need the '\0' terminator.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author
#3 lore raw
Quoted 15 lines · David Laight

From: Chang-Hsien Tsai

samples/bpf/bpf_load.c void read_trace_pipe(void) · line 684

Quoted 9 lines
samples/bpf/bpf_load.c +1 -1
@@ -678,7 +678,7 @@ void read_trace_pipe(void)
              static char buf[4096];
              ssize_t sz;
 
-             sz = read(trace_fd, buf, sizeof(buf));
+             sz = read(trace_fd, buf, sizeof(buf)-1);
              if (sz > 0) {
                      buf[sz] = 0;
                      puts(buf);

Why not change the puts() to fwrite(buf, sz, 1, stdout) ? Then you don't need the '\0' terminator.

Quoted 6 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

Yes.
But, this will be different with the original code. puts(buf) prints buf and a terminating newline character.

--
Chang-Hsien Tsai