Fix realloc() with size 0

Valgrind reported “realloc() with size 0"

'''
==508== realloc() with size 0
==508== at 0x484DB80: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==508== by 0x1093E3: ringbuffer_consumer_unregister (ringbuffer.c:103)
==508== by 0x10942B: ringbuffer_fini (ringbuffer.c:48)
==508== by 0x109989: test_simple_poll (test-ringbuffer-simple-poll.c:31)
==508== by 0x109A3A: main (test-ringbuffer-simple-poll.c:37)
'''

And gnu libc manual[1] said:
Portable programs should not attempt to reallocate blocks to be size
zero. On other implementations if ptr is non-null, realloc (ptr, 0)
might free the block and return a non-null pointer to a size-zero
object, or it might fail and return NULL without freeing the block.
The ISO C17 standard allows these variations.

So use free instead of realloc with size 0

[1] https://www.gnu.org/software/libc/manual/html_node/Changing-Block-Size.html
Change-Id: Ie716dd6ea84696a2091c680d71a0624423d1a185
Signed-off-by: John Wang <wangzhiqiang02@ieisystem.com>
1 file changed
tree: e73029c738cf7f2c571af30f49f7f8493afa6949
  1. conf/
  2. test/
  3. .clang-format
  4. .clang-tidy
  5. .gitignore
  6. .travis.yml
  7. CHANGELOG.md
  8. config.c
  9. console-client.c
  10. console-dbus.c
  11. console-server.c
  12. console-server.h
  13. console-socket.c
  14. LICENSE
  15. log-handler.c
  16. meson.build
  17. meson.options
  18. OWNERS
  19. README.md
  20. ringbuffer.c
  21. socket-handler.c
  22. tty-handler.c
  23. util.c
README.md

To Build

To build this project, run the following shell commands:

meson setup build
meson compile -C build

To test:

meson test -C build

To Run Server

Running the server requires a serial port (e.g. /dev/ttyS0):

touch obmc-console.conf
./obmc-console-server --config obmc-console.conf ttyS0

To Connect Client

To connect to the server, simply run the client:

./obmc-console-client

To disconnect the client, use the standard ~. combination.

Underlying design

This shows how the host UART connection is abstracted within the BMC as a Unix domain socket.

               +---------------------------------------------------------------------------------------------+
               |                                                                                             |
               |       obmc-console-client       unix domain socket         obmc-console-server              |
               |                                                                                             |
               |     +----------------------+                           +------------------------+           |
               |     |   client.2200.conf   |  +---------------------+  | server.ttyVUART0.conf  |           |
           +---+--+  +----------------------+  |                     |  +------------------------+  +--------+-------+
Network    | 2200 +-->                      +->+ @obmc-console.host0 +<-+                        <--+ /dev/ttyVUART0 |   UARTs
           +---+--+  | console-id = "host0" |  |                     |  |  console-id = "host0"  |  +--------+-------+
               |     |                      |  +---------------------+  |                        |           |
               |     +----------------------+                           +------------------------+           |
               |                                                                                             |
               |                                                                                             |
               |                                                                                             |
               +---------------------------------------------------------------------------------------------+

This supports multiple independent consoles. The console-id is a unique portion for the unix domain socket created by the obmc-console-server instance. The server needs to know this because it needs to know what to name the pipe; the client needs to know it as it needs to form the abstract socket name to which to connect.