Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kvlist: add _s APIs to pass str length as arg #36

Merged
merged 4 commits into from
Feb 25, 2024

Conversation

nokute78
Copy link
Contributor

@nokute78 nokute78 commented May 27, 2023

This patch is to add cfl_kvlist_insert_*_s APIs and tests.
These API is to pass a length of key / (value for insert_string).
It is useful to pass non-NULL-terminated string like msgpack_object_str

This patch also adds cfl_variant_create_from_string_s.
It is needed by cfl_kvlist_insert_string_s to create value.

Valgrind output

taka@taka-VirtualBox:~/git/cfl/build$ valgrind --leak-check=full --show-leak-kinds=all tests/cfl-test-kvlist 
==25106== Memcheck, a memory error detector
==25106== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==25106== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==25106== Command: tests/cfl-test-kvlist
==25106== 
Test create_destroy...                          [   OK   ]
==25107== 
==25107== HEAP SUMMARY:
==25107==     in use at exit: 216 bytes in 2 blocks
==25107==   total heap usage: 4 allocs, 2 frees, 1,256 bytes allocated
==25107== 
==25107== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25107==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25107==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25107== 
==25107== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25107==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25107==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25107== 
==25107== LEAK SUMMARY:
==25107==    definitely lost: 0 bytes in 0 blocks
==25107==    indirectly lost: 0 bytes in 0 blocks
==25107==      possibly lost: 0 bytes in 0 blocks
==25107==    still reachable: 216 bytes in 2 blocks
==25107==         suppressed: 0 bytes in 0 blocks
==25107== 
==25107== For lists of detected and suppressed errors, rerun with: -s
==25107== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test count...                                   [   OK   ]
==25108== 
==25108== HEAP SUMMARY:
==25108==     in use at exit: 216 bytes in 2 blocks
==25108==   total heap usage: 40 allocs, 38 frees, 2,050 bytes allocated
==25108== 
==25108== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25108==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25108==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25108== 
==25108== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25108==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25108==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25108== 
==25108== LEAK SUMMARY:
==25108==    definitely lost: 0 bytes in 0 blocks
==25108==    indirectly lost: 0 bytes in 0 blocks
==25108==      possibly lost: 0 bytes in 0 blocks
==25108==    still reachable: 216 bytes in 2 blocks
==25108==         suppressed: 0 bytes in 0 blocks
==25108== 
==25108== For lists of detected and suppressed errors, rerun with: -s
==25108== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test fetch...                                   [   OK   ]
==25109== 
==25109== HEAP SUMMARY:
==25109==     in use at exit: 216 bytes in 2 blocks
==25109==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25109== 
==25109== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25109==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25109==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25109== 
==25109== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25109==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25109==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25109== 
==25109== LEAK SUMMARY:
==25109==    definitely lost: 0 bytes in 0 blocks
==25109==    indirectly lost: 0 bytes in 0 blocks
==25109==      possibly lost: 0 bytes in 0 blocks
==25109==    still reachable: 216 bytes in 2 blocks
==25109==         suppressed: 0 bytes in 0 blocks
==25109== 
==25109== For lists of detected and suppressed errors, rerun with: -s
==25109== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_string...                           [   OK   ]
==25111== 
==25111== HEAP SUMMARY:
==25111==     in use at exit: 216 bytes in 2 blocks
==25111==   total heap usage: 8 allocs, 6 frees, 1,346 bytes allocated
==25111== 
==25111== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25111==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25111==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25111== 
==25111== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25111==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25111==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25111== 
==25111== LEAK SUMMARY:
==25111==    definitely lost: 0 bytes in 0 blocks
==25111==    indirectly lost: 0 bytes in 0 blocks
==25111==      possibly lost: 0 bytes in 0 blocks
==25111==    still reachable: 216 bytes in 2 blocks
==25111==         suppressed: 0 bytes in 0 blocks
==25111== 
==25111== For lists of detected and suppressed errors, rerun with: -s
==25111== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_bytes...                            [   OK   ]
==25112== 
==25112== HEAP SUMMARY:
==25112==     in use at exit: 216 bytes in 2 blocks
==25112==   total heap usage: 8 allocs, 6 frees, 1,346 bytes allocated
==25112== 
==25112== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25112==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25112==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25112== 
==25112== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25112==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25112==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25112== 
==25112== LEAK SUMMARY:
==25112==    definitely lost: 0 bytes in 0 blocks
==25112==    indirectly lost: 0 bytes in 0 blocks
==25112==      possibly lost: 0 bytes in 0 blocks
==25112==    still reachable: 216 bytes in 2 blocks
==25112==         suppressed: 0 bytes in 0 blocks
==25112== 
==25112== For lists of detected and suppressed errors, rerun with: -s
==25112== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_reference...                        [   OK   ]
==25113== 
==25113== HEAP SUMMARY:
==25113==     in use at exit: 216 bytes in 2 blocks
==25113==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25113== 
==25113== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25113==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25113==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25113== 
==25113== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25113==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25113==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25113== 
==25113== LEAK SUMMARY:
==25113==    definitely lost: 0 bytes in 0 blocks
==25113==    indirectly lost: 0 bytes in 0 blocks
==25113==      possibly lost: 0 bytes in 0 blocks
==25113==    still reachable: 216 bytes in 2 blocks
==25113==         suppressed: 0 bytes in 0 blocks
==25113== 
==25113== For lists of detected and suppressed errors, rerun with: -s
==25113== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_bool...                             [   OK   ]
==25114== 
==25114== HEAP SUMMARY:
==25114==     in use at exit: 216 bytes in 2 blocks
==25114==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25114== 
==25114== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25114==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25114==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25114== 
==25114== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25114==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25114==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25114== 
==25114== LEAK SUMMARY:
==25114==    definitely lost: 0 bytes in 0 blocks
==25114==    indirectly lost: 0 bytes in 0 blocks
==25114==      possibly lost: 0 bytes in 0 blocks
==25114==    still reachable: 216 bytes in 2 blocks
==25114==         suppressed: 0 bytes in 0 blocks
==25114== 
==25114== For lists of detected and suppressed errors, rerun with: -s
==25114== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_int64...                            [   OK   ]
==25115== 
==25115== HEAP SUMMARY:
==25115==     in use at exit: 216 bytes in 2 blocks
==25115==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25115== 
==25115== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25115==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25115==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25115== 
==25115== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25115==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25115==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25115== 
==25115== LEAK SUMMARY:
==25115==    definitely lost: 0 bytes in 0 blocks
==25115==    indirectly lost: 0 bytes in 0 blocks
==25115==      possibly lost: 0 bytes in 0 blocks
==25115==    still reachable: 216 bytes in 2 blocks
==25115==         suppressed: 0 bytes in 0 blocks
==25115== 
==25115== For lists of detected and suppressed errors, rerun with: -s
==25115== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_uint64...                           [   OK   ]
==25116== 
==25116== HEAP SUMMARY:
==25116==     in use at exit: 216 bytes in 2 blocks
==25116==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25116== 
==25116== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25116==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25116==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25116== 
==25116== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25116==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25116==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25116== 
==25116== LEAK SUMMARY:
==25116==    definitely lost: 0 bytes in 0 blocks
==25116==    indirectly lost: 0 bytes in 0 blocks
==25116==      possibly lost: 0 bytes in 0 blocks
==25116==    still reachable: 216 bytes in 2 blocks
==25116==         suppressed: 0 bytes in 0 blocks
==25116== 
==25116== For lists of detected and suppressed errors, rerun with: -s
==25116== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_double...                           [   OK   ]
==25117== 
==25117== HEAP SUMMARY:
==25117==     in use at exit: 216 bytes in 2 blocks
==25117==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25117== 
==25117== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25117==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25117==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25117== 
==25117== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25117==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25117==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25117== 
==25117== LEAK SUMMARY:
==25117==    definitely lost: 0 bytes in 0 blocks
==25117==    indirectly lost: 0 bytes in 0 blocks
==25117==      possibly lost: 0 bytes in 0 blocks
==25117==    still reachable: 216 bytes in 2 blocks
==25117==         suppressed: 0 bytes in 0 blocks
==25117== 
==25117== For lists of detected and suppressed errors, rerun with: -s
==25117== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_new_array...                        [   OK   ]
==25118== 
==25118== HEAP SUMMARY:
==25118==     in use at exit: 216 bytes in 2 blocks
==25118==   total heap usage: 9 allocs, 7 frees, 2,340 bytes allocated
==25118== 
==25118== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25118==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25118==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25118== 
==25118== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25118==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25118==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25118== 
==25118== LEAK SUMMARY:
==25118==    definitely lost: 0 bytes in 0 blocks
==25118==    indirectly lost: 0 bytes in 0 blocks
==25118==      possibly lost: 0 bytes in 0 blocks
==25118==    still reachable: 216 bytes in 2 blocks
==25118==         suppressed: 0 bytes in 0 blocks
==25118== 
==25118== For lists of detected and suppressed errors, rerun with: -s
==25118== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_empty_array...                      [   OK   ]
==25119== 
==25119== HEAP SUMMARY:
==25119==     in use at exit: 216 bytes in 2 blocks
==25119==   total heap usage: 9 allocs, 7 frees, 2,340 bytes allocated
==25119== 
==25119== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25119==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25119==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25119== 
==25119== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25119==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25119==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25119== 
==25119== LEAK SUMMARY:
==25119==    definitely lost: 0 bytes in 0 blocks
==25119==    indirectly lost: 0 bytes in 0 blocks
==25119==      possibly lost: 0 bytes in 0 blocks
==25119==    still reachable: 216 bytes in 2 blocks
==25119==         suppressed: 0 bytes in 0 blocks
==25119== 
==25119== For lists of detected and suppressed errors, rerun with: -s
==25119== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_empty_kvlist...                     [   OK   ]
==25120== 
==25120== HEAP SUMMARY:
==25120==     in use at exit: 216 bytes in 2 blocks
==25120==   total heap usage: 8 allocs, 6 frees, 1,340 bytes allocated
==25120== 
==25120== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25120==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25120==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25120== 
==25120== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25120==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25120==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25120== 
==25120== LEAK SUMMARY:
==25120==    definitely lost: 0 bytes in 0 blocks
==25120==    indirectly lost: 0 bytes in 0 blocks
==25120==      possibly lost: 0 bytes in 0 blocks
==25120==    still reachable: 216 bytes in 2 blocks
==25120==         suppressed: 0 bytes in 0 blocks
==25120== 
==25120== For lists of detected and suppressed errors, rerun with: -s
==25120== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test fetch_s...                                 [   OK   ]
==25121== 
==25121== HEAP SUMMARY:
==25121==     in use at exit: 216 bytes in 2 blocks
==25121==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25121== 
==25121== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25121==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25121==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25121== 
==25121== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25121==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25121==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25121== 
==25121== LEAK SUMMARY:
==25121==    definitely lost: 0 bytes in 0 blocks
==25121==    indirectly lost: 0 bytes in 0 blocks
==25121==      possibly lost: 0 bytes in 0 blocks
==25121==    still reachable: 216 bytes in 2 blocks
==25121==         suppressed: 0 bytes in 0 blocks
==25121== 
==25121== For lists of detected and suppressed errors, rerun with: -s
==25121== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_string_s...                         [   OK   ]
==25122== 
==25122== HEAP SUMMARY:
==25122==     in use at exit: 216 bytes in 2 blocks
==25122==   total heap usage: 8 allocs, 6 frees, 1,346 bytes allocated
==25122== 
==25122== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25122==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25122==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25122== 
==25122== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25122==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25122==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25122== 
==25122== LEAK SUMMARY:
==25122==    definitely lost: 0 bytes in 0 blocks
==25122==    indirectly lost: 0 bytes in 0 blocks
==25122==      possibly lost: 0 bytes in 0 blocks
==25122==    still reachable: 216 bytes in 2 blocks
==25122==         suppressed: 0 bytes in 0 blocks
==25122== 
==25122== For lists of detected and suppressed errors, rerun with: -s
==25122== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_bytes_s...                          [   OK   ]
==25123== 
==25123== HEAP SUMMARY:
==25123==     in use at exit: 216 bytes in 2 blocks
==25123==   total heap usage: 8 allocs, 6 frees, 1,346 bytes allocated
==25123== 
==25123== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25123==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25123==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25123== 
==25123== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25123==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25123==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25123== 
==25123== LEAK SUMMARY:
==25123==    definitely lost: 0 bytes in 0 blocks
==25123==    indirectly lost: 0 bytes in 0 blocks
==25123==      possibly lost: 0 bytes in 0 blocks
==25123==    still reachable: 216 bytes in 2 blocks
==25123==         suppressed: 0 bytes in 0 blocks
==25123== 
==25123== For lists of detected and suppressed errors, rerun with: -s
==25123== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_reference_s...                      [   OK   ]
==25124== 
==25124== HEAP SUMMARY:
==25124==     in use at exit: 216 bytes in 2 blocks
==25124==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25124== 
==25124== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25124==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25124==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25124== 
==25124== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25124==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25124==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25124== 
==25124== LEAK SUMMARY:
==25124==    definitely lost: 0 bytes in 0 blocks
==25124==    indirectly lost: 0 bytes in 0 blocks
==25124==      possibly lost: 0 bytes in 0 blocks
==25124==    still reachable: 216 bytes in 2 blocks
==25124==         suppressed: 0 bytes in 0 blocks
==25124== 
==25124== For lists of detected and suppressed errors, rerun with: -s
==25124== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_bool_s...                           [   OK   ]
==25125== 
==25125== HEAP SUMMARY:
==25125==     in use at exit: 216 bytes in 2 blocks
==25125==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25125== 
==25125== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25125==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25125==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25125== 
==25125== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25125==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25125==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25125== 
==25125== LEAK SUMMARY:
==25125==    definitely lost: 0 bytes in 0 blocks
==25125==    indirectly lost: 0 bytes in 0 blocks
==25125==      possibly lost: 0 bytes in 0 blocks
==25125==    still reachable: 216 bytes in 2 blocks
==25125==         suppressed: 0 bytes in 0 blocks
==25125== 
==25125== For lists of detected and suppressed errors, rerun with: -s
==25125== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_int64_s...                          [   OK   ]
==25126== 
==25126== HEAP SUMMARY:
==25126==     in use at exit: 216 bytes in 2 blocks
==25126==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25126== 
==25126== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25126==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25126==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25126== 
==25126== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25126==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25126==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25126== 
==25126== LEAK SUMMARY:
==25126==    definitely lost: 0 bytes in 0 blocks
==25126==    indirectly lost: 0 bytes in 0 blocks
==25126==      possibly lost: 0 bytes in 0 blocks
==25126==    still reachable: 216 bytes in 2 blocks
==25126==         suppressed: 0 bytes in 0 blocks
==25126== 
==25126== For lists of detected and suppressed errors, rerun with: -s
==25126== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_uint64_s...                         [   OK   ]
==25127== 
==25127== HEAP SUMMARY:
==25127==     in use at exit: 216 bytes in 2 blocks
==25127==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25127== 
==25127== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25127==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25127==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25127== 
==25127== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25127==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25127==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25127== 
==25127== LEAK SUMMARY:
==25127==    definitely lost: 0 bytes in 0 blocks
==25127==    indirectly lost: 0 bytes in 0 blocks
==25127==      possibly lost: 0 bytes in 0 blocks
==25127==    still reachable: 216 bytes in 2 blocks
==25127==         suppressed: 0 bytes in 0 blocks
==25127== 
==25127== For lists of detected and suppressed errors, rerun with: -s
==25127== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_double_s...                         [   OK   ]
==25128== 
==25128== HEAP SUMMARY:
==25128==     in use at exit: 216 bytes in 2 blocks
==25128==   total heap usage: 7 allocs, 5 frees, 1,324 bytes allocated
==25128== 
==25128== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25128==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25128==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25128== 
==25128== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25128==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25128==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25128== 
==25128== LEAK SUMMARY:
==25128==    definitely lost: 0 bytes in 0 blocks
==25128==    indirectly lost: 0 bytes in 0 blocks
==25128==      possibly lost: 0 bytes in 0 blocks
==25128==    still reachable: 216 bytes in 2 blocks
==25128==         suppressed: 0 bytes in 0 blocks
==25128== 
==25128== For lists of detected and suppressed errors, rerun with: -s
==25128== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_new_array_s...                      [   OK   ]
==25129== 
==25129== HEAP SUMMARY:
==25129==     in use at exit: 216 bytes in 2 blocks
==25129==   total heap usage: 9 allocs, 7 frees, 2,340 bytes allocated
==25129== 
==25129== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25129==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25129==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25129== 
==25129== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25129==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25129==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25129== 
==25129== LEAK SUMMARY:
==25129==    definitely lost: 0 bytes in 0 blocks
==25129==    indirectly lost: 0 bytes in 0 blocks
==25129==      possibly lost: 0 bytes in 0 blocks
==25129==    still reachable: 216 bytes in 2 blocks
==25129==         suppressed: 0 bytes in 0 blocks
==25129== 
==25129== For lists of detected and suppressed errors, rerun with: -s
==25129== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_empty_array_s...                    [   OK   ]
==25130== 
==25130== HEAP SUMMARY:
==25130==     in use at exit: 216 bytes in 2 blocks
==25130==   total heap usage: 9 allocs, 7 frees, 2,340 bytes allocated
==25130== 
==25130== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25130==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25130==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25130== 
==25130== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25130==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25130==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25130== 
==25130== LEAK SUMMARY:
==25130==    definitely lost: 0 bytes in 0 blocks
==25130==    indirectly lost: 0 bytes in 0 blocks
==25130==      possibly lost: 0 bytes in 0 blocks
==25130==    still reachable: 216 bytes in 2 blocks
==25130==         suppressed: 0 bytes in 0 blocks
==25130== 
==25130== For lists of detected and suppressed errors, rerun with: -s
==25130== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Test insert_empty_kvlist_s...                   [   OK   ]
==25131== 
==25131== HEAP SUMMARY:
==25131==     in use at exit: 216 bytes in 2 blocks
==25131==   total heap usage: 8 allocs, 6 frees, 1,340 bytes allocated
==25131== 
==25131== 24 bytes in 1 blocks are still reachable in loss record 1 of 2
==25131==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25131==    by 0x10A530: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25131== 
==25131== 192 bytes in 1 blocks are still reachable in loss record 2 of 2
==25131==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==25131==    by 0x10A514: main (in /home/taka/git/cfl/build/tests/cfl-test-kvlist)
==25131== 
==25131== LEAK SUMMARY:
==25131==    definitely lost: 0 bytes in 0 blocks
==25131==    indirectly lost: 0 bytes in 0 blocks
==25131==      possibly lost: 0 bytes in 0 blocks
==25131==    still reachable: 216 bytes in 2 blocks
==25131==         suppressed: 0 bytes in 0 blocks
==25131== 
==25131== For lists of detected and suppressed errors, rerun with: -s
==25131== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
SUCCESS: All unit tests have passed.
==25106== 
==25106== HEAP SUMMARY:
==25106==     in use at exit: 0 bytes in 0 blocks
==25106==   total heap usage: 3 allocs, 3 frees, 1,240 bytes allocated
==25106== 
==25106== All heap blocks were freed -- no leaks are possible
==25106== 
==25106== For lists of detected and suppressed errors, rerun with: -s
==25106== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
taka@taka-VirtualBox:~/git/cfl/build$ 

@leonardo-albertovich
Copy link
Contributor

leonardo-albertovich commented May 27, 2023

Could you please explain what's the rationale behind this? Do we need to use non null terminated binary chunks of data anywhere?

I can see how a decoded msgpack string would not be null terminated but I want to evaluate the pros / cons of the change.

@nokute78
Copy link
Contributor Author

Pros

  1. Memory safety
  2. Handling non-null-terminated string
  3. Reducing strlen cost

1. Memory safety

Current APIs can cause out of bounds memory access.
These APIs read memory until a null character appears.
If user passes non-null-terminated string, these APIs will read out of bounds.
It can cause SIGSEGV or reading sensitive data.
https://cwe.mitre.org/data/definitions/125.html

New APIs prevents it since we can pass a length of string.

2. Handling non-null-terminated string

We can set non-null-terminated string as a key of kvlist since we can pass a length of the string.

As you know, msgpack-c string is not null-terminated.
If we use current API, we have to create temporary buffer, copy from msgpack-c string and append a null character to construct null-terminated string from msgpack-c string.

3. Reducing strlen cost

Current API creates cfl_sds_t using cfl_sds_create which calls strlen and cfl_sds_create_len.
On the other hand, new API creates cfl_sds_t using cfl_sds_create_len.
It will reduce strlen cost.

Cons

It is redundant if we pass const string.
e.g. https://github.com/fluent/fluent-bit/blob/v2.1.4/src/config_format/flb_cf_yaml.c#L862

In such case, it is better to use current API.

@leonardo-albertovich
Copy link
Contributor

The real question is how much sense it makes to pass that complexity cost along to the client code and that depends on what the client code is which is what I want to have a clear picture on because in most cases I have observed that the pattern was misunderstood and blindly followed which caused the client code to be unnecessarily bulkier.

Is there any clarity on how much code is performing memory copy operations for this? I don't have anything against having this as an option and we might need it in the future but I don't want to mindlessly add it and cause confusion.

Even though it's true that passing a non null terminated buffer to a function that expects one could cause an information leak or even an invalid memory access if it's close enough to the end of a mapping that has no adjacent mappings that is not the real answer. The real answer is to be mindful and understand the constraints and data types the functions you are working with expect.

@nokute78
Copy link
Contributor Author

nokute78 commented May 27, 2023

https://wiki.sei.cmu.edu/confluence/display/c/STR07-C.+Use+the+bounds-checking+interfaces+for+string+manipulation
Some bounds checking functions were defined from C11 (optional). It is a modern standard way.
e.g.
errno_t strcpy_s(char * restrict s1, rsize_t s1max, const char * restrict s2); as an alternative to strcpy.

I'm concerned about the proliferation of the use of something that shouldn't be used.

Memory copy operations of inserting

Is there any clarity on how much code is performing memory copy operations for this?

Current inserting API will increase 16 or 48 bytes memory copy operations.
This is an example calling current API cfl_kvlist_insert_int64 with this PR.

  1. cfl_kvlist_insert_int64 (+32 + 8 bytes, +stack +strlen cost) can be removed adding inline or using macro.
  2. cfl_kvlist_insert_int64_s (+8bytes)
  3. cfl_kvlist_insert_s (+8bytes, -8bytes, -strlen cost)

1. cfl_kvlist_insert_int64

Current insert functions will be changed like this

int cfl_kvlist_insert_int64(struct cfl_kvlist *list,
                            char *key, int64_t value)
{
    return cfl_kvlist_insert_int64_s(list, key, strlen(key), value);
}

Current API calls new API with strlen(key) and these args are appended memory copy operations. 32bytes(arg) + 8bytes (arg of strlen).
This can be removed add inline.

2. cfl_kvlist_insert_int64_s

The difference of new APIs is calling cfl_kvlist_insert_s here
It also appends 8 byte memory copy operations.

3. cfl_kvlist_insert_s

cfl_kvlist_insert_s calls cfl_sds_create_len instead of cfl_sds_create here
It also appends 8 byte memory copy operations by this function calling.
However it also reduces 8 byte copy operations and strlen since cfl_sds_create calls strlen and call cfl_sds_create_len.
https://github.com/fluent/cfl/blob/v0.2.3/src/cfl_sds.c#L113

Memory copy operations of fetching

Current fetching API will increase 8 or 32 bytes memory copy operations.

  1. cfl_kvlist_fetch (+16 + 8bytes + stack + strlen cost) can be removed adding inline or using macro.
  2. cfl_kvlist_fetch_s (+8bytes , -strlen cost + strncmp - strcpy cost)

1. cfl_kvlist_fetch

Current API will be changed like this

struct cfl_variant *cfl_kvlist_fetch(struct cfl_kvlist *list, char *key)
{
    return cfl_kvlist_fetch_s(list, key, strlen(key));
}

Current API calls new API with strlen(key) and these args are appended memory copy operations. 32bytes(arg) + 8bytes(arg of strlen).
This can be removed add inline.

2. cfl_kvlist_fetch_s

The new API calls strncmp instead of strcmp. It needs 8byte as an arg of strncmp.

My patch removes strlen since the new API supports length of string.

@leonardo-albertovich
Copy link
Contributor

I'm not worried about the additional shadow stack space used, what I want to know is if there is existing client code that has to manually copy and terminate non null terminated strings because these functions do not exist.

What I don't want is to confuse people into thinking that they need to use the _s version of the functions even when they know they are handling null terminated strings because that makes no sense.

@nokute78
Copy link
Contributor Author

So why does cfl_kv support cfl_kv_item_create_len ?

struct cfl_kv *cfl_kv_item_create_len(struct cfl_list *list,
                                      char *k_buf, size_t k_len,
                                      char *v_buf, size_t v_len)

It is already using at flb_signv4.
https://github.com/fluent/fluent-bit/blob/v2.1.4/src/flb_signv4.c#L380-L382
Is it confusing ?

cfl_sds also supports cfl_sds_create(const char *str) and cfl_sds_create_len(const char *str, int len)

How about cfl_kvlist_*_len instead of cfl_kvlist_*_s ?

@leonardo-albertovich
Copy link
Contributor

That seems to be because it's using flb_kv (which was ported as cfl_kv) which is an older kv list implementation that's mostly for strings but also supports holding references (if you set the value length to zero).

That's not connected to the cfl variant or any associated types. In the cfl variant family strings are null terminated (because we use UTF-8) and binary data storage requires the length to be specified.

It's not that I have something against this in particular but I would like have a very clear picture of why do we need or want this.

@nokute78
Copy link
Contributor Author

I want to refactor filter_record_modifier by using new APIs.

Currently filter_record_modifier defines own kv store.
It is better to replace common data store like cfl_kvlist, cfl_kv or flb_kv.
https://github.com/fluent/fluent-bit/blob/v2.1.4/plugins/filter_record_modifier/filter_modifier.h#L27-L33

It is used to store properties of filter_record_modifier.
e.g.
https://github.com/fluent/fluent-bit/blob/v2.1.4/plugins/filter_record_modifier/filter_modifier.c#L102-L123
It can be replaced using cfl_kvlist_insert_string_s.

It also is used to check record using msgpack string.
https://github.com/fluent/fluent-bit/blob/v2.1.4/plugins/filter_record_modifier/filter_modifier.c#L250-L266
It can be replaced using cfl_kvlist_fetch_s.
However current API will not work since there is no API to pass the length of msgpack string.

I believe these API is useful for such cases and improving maintainability by using cfl APIs.

@leonardo-albertovich
Copy link
Contributor

For your use case you are better off using cfl_kv which already lets you specify those lengths, is already used to store opaque references in the value sometimes and doesn't bring in all the variant machinery.

@nokute78
Copy link
Contributor Author

Thank you for comment.
cfl_kv supports cfl_kv_item_create_len, however it doesn't support an API to get value using length...

@leonardo-albertovich
Copy link
Contributor

The reason why I'd stay away from variants in most cases is they are lacking in ergonomics and I didn't really "design" them for general use at the time, it was a reaction to a particular need (otlp attributes).

Anyway, as you mentioned they were adopted for other uses so we need to improve their ergonomics, the quality (I particularly dislike the kvlist implementation) and maybe extend them (do we want a cfl_variant aware / native hash map or list?)

Maybe those are some questions we could us to start a wider discussion in order to turn variants into first class citizens but that doesn't have anything to do with this PR. I have already approved it so we don't get stuck in a pointless argument.

@edsiper
Copy link
Member

edsiper commented Feb 25, 2024

Getting merged this to get the functionality, I will check in another PR potential fixes for CI tests

@edsiper edsiper merged commit e7b4cbc into fluent:master Feb 25, 2024
5 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants