ricardgb opened a new pull request, #22452:
URL: https://github.com/apache/nuttx/pull/22452

   ## Summary
   
   Fixes #22451.
   
   `netprocfs_read_tcpstats()` and `netprocfs_read_udpstats()` wrote the header 
of the table in
   the buffer of the caller with `snprintf()` and went on with the length that 
it returns, the
   length of the whole header also when the buffer is shorter. `buflen - len` 
then wrapped, and
   all the lines of the connections were written past the end of the buffer.
   
   * Each line (the header and the line of a connection) is now written in 
`priv->line` and
     transferred with the new `netprocfs_read_line()`, as much as fits; the 
rest of the line stays
     buffered for the next read. This also ends the early end of the file for 
buffers shorter than
     a line.
   * `NET_LINELEN` grows from 80 to the length of a line of these tables (180 
with IPv6, 120
     without); `TCP_LINELEN` and `UDP_LINELEN` go away.
   * The walk of the connections, its locks and the format of the lines do not 
change.
   
   ## Impact
   
   Readers of `/proc/net/tcp` and `/proc/net/udp` (`CONFIG_NET_STATISTICS`) 
with a buffer shorter
   than 180 bytes, which includes every stdio reader. `struct netprocfs_file_s` 
(one per open file
   of `/proc/net`) grows by 100 bytes with IPv6, 40 without. No change of the 
contents.
   
   ## Testing
   
   Host: Ubuntu, GCC. Master `a8bb89bf680`, `sim:tcploop` + `EXAMPLES_HELLO`, 
`NET_STATISTICS`,
   `NET_UDP`.
   
   Reproducer of the issue (one `read()` of 64 bytes, guard area after the 
buffer):
   
   | | /proc/net/tcp | /proc/net/udp |
   |---|---|---|
   | master | returned 289, 226 bytes past the buffer | returned 148, 85 bytes 
past the buffer |
   | this PR | returned 64, 0 bytes past the buffer | returned 64, 0 bytes past 
the buffer |
   
   Whole file read with buffers of 1, 7, 63, 64, 65, 100, 179, 180, 181, 300 
and 512 bytes,
   compared with one read of 4095 bytes (6 TCP connections, 693 bytes; 3 UDP, 
310 bytes):
   
   | | Result |
   |---|---|
   | master | 1 to 65: `read()` returns the whole table (693 / 310) for any 
size; 100 and 179: header only (87 / 67 bytes); 180 and more: right |
   | this PR | the same contents with every size |
   
   `cat /proc/net/tcp` prints the same table as before. `./tools/checkpatch.sh 
-g HEAD~1..HEAD`:
   all checks pass.
   
   *Disclosure: this change was prepared with the help of an AI agent (Claude 
Code, Anthropic); it
   was tested in the simulator and human-reviewed.*
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to