clang warnings from coverity
Coverity reported the following error using clang analyzers:
1. libcacard-2.7.0/src/simpletlv.c:122:17: warning: Null pointer passed as an argument to a 'nonnull' parameter
# memcpy(p, tlv[i].value.value, tlv[i].length);
# ^
4. libcacard-2.7.0/src/simpletlv.c:166:12: note: Calling 'simpletlv_encode_internal'
# return simpletlv_encode_internal(tlv, tlv_len, out, outlen, newptr,
# ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7. libcacard-2.7.0/src/simpletlv.c:80:55: note: 'newp' initialized to a null pointer value
# unsigned char *tmp = NULL, *a = NULL, *p = NULL, *newp = NULL;
# ^~~~
10. libcacard-2.7.0/src/simpletlv.c:85:9: note: Assuming 'expect_len' is > 0
# if (expect_len <= 0)
# ^~~~~~~~~~~~~~~
13. libcacard-2.7.0/src/simpletlv.c:85:5: note: Taking false branch
# if (expect_len <= 0)
# ^
16. libcacard-2.7.0/src/simpletlv.c:88:9: note: Assuming 'outlen' is not equal to 0
# if (outlen == 0 && out != NULL) {
# ^~~~~~~~~~~
19. libcacard-2.7.0/src/simpletlv.c:88:21: note: Left side of '&&' is false
# if (outlen == 0 && out != NULL) {
# ^
22. libcacard-2.7.0/src/simpletlv.c:93:16: note: Assuming 'outlen' is >= 'expect_len'
# } else if ((int)outlen >= expect_len && out != NULL) {
# ^~~~~~~~~~~~~~~~~~~~~~~~~
25. libcacard-2.7.0/src/simpletlv.c:93:16: note: Left side of '&&' is true
26. libcacard-2.7.0/src/simpletlv.c:93:45: note: Assuming 'out' is not equal to NULL
# } else if ((int)outlen >= expect_len && out != NULL) {
# ^~~~~~~~~~~
29. libcacard-2.7.0/src/simpletlv.c:93:12: note: Taking true branch
# } else if ((int)outlen >= expect_len && out != NULL) {
# ^
32. libcacard-2.7.0/src/simpletlv.c:102:5: note: Loop condition is true. Entering loop body
# for (i = 0; i < tlv_len; i++) {
# ^
35. libcacard-2.7.0/src/simpletlv.c:106:9: note: Taking false branch
# if (tlv[i].type == SIMPLETLV_TYPE_NONE)
# ^
38. libcacard-2.7.0/src/simpletlv.c:109:9: note: Taking false branch
# if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND) {
# ^
41. libcacard-2.7.0/src/simpletlv.c:113:9: note: Taking false branch
# if (buffer_type & SIMPLETLV_TL) {
# ^
44. libcacard-2.7.0/src/simpletlv.c:120:9: note: Taking true branch
# if (buffer_type & SIMPLETLV_VALUE) {
# ^
47. libcacard-2.7.0/src/simpletlv.c:121:17: note: Assuming the condition is false
# if (tlv[i].type == SIMPLETLV_TYPE_LEAF) {
# ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50. libcacard-2.7.0/src/simpletlv.c:121:13: note: Taking false branch
# if (tlv[i].type == SIMPLETLV_TYPE_LEAF) {
# ^
53. libcacard-2.7.0/src/simpletlv.c:126:22: note: Calling 'simpletlv_encode_internal'
# rv = simpletlv_encode_internal(tlv[i].value.child,
# ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56. libcacard-2.7.0/src/simpletlv.c:85:5: note: Taking true branch
# if (expect_len <= 0)
# ^
59. libcacard-2.7.0/src/simpletlv.c:86:9: note: Returning without writing to '*newptr'
# return expect_len;
# ^
62. libcacard-2.7.0/src/simpletlv.c:86:9: note: Returning without writing to '*out'
63. libcacard-2.7.0/src/simpletlv.c:126:22: note: Returning from 'simpletlv_encode_internal'
# rv = simpletlv_encode_internal(tlv[i].value.child,
# ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66. libcacard-2.7.0/src/simpletlv.c:128:17: note: Taking false branch
# if (rv < 0)
# ^
69. libcacard-2.7.0/src/simpletlv.c:130:17: note: Null pointer value stored to 'p'
# p = newp;
# ^~~~~~~~
72. libcacard-2.7.0/src/simpletlv.c:102:5: note: Loop condition is true. Entering loop body
# for (i = 0; i < tlv_len; i++) {
# ^
75. libcacard-2.7.0/src/simpletlv.c:106:9: note: Taking false branch
# if (tlv[i].type == SIMPLETLV_TYPE_NONE)
# ^
78. libcacard-2.7.0/src/simpletlv.c:109:9: note: Taking false branch
# if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND) {
# ^
81. libcacard-2.7.0/src/simpletlv.c:113:9: note: Taking false branch
# if (buffer_type & SIMPLETLV_TL) {
# ^
84. libcacard-2.7.0/src/simpletlv.c:120:9: note: Taking true branch
# if (buffer_type & SIMPLETLV_VALUE) {
# ^
87. libcacard-2.7.0/src/simpletlv.c:121:17: note: Assuming the condition is true
# if (tlv[i].type == SIMPLETLV_TYPE_LEAF) {
# ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90. libcacard-2.7.0/src/simpletlv.c:121:13: note: Taking true branch
# if (tlv[i].type == SIMPLETLV_TYPE_LEAF) {
# ^
93. libcacard-2.7.0/src/simpletlv.c:122:17: note: Null pointer passed as an argument to a 'nonnull' parameter
# memcpy(p, tlv[i].value.value, tlv[i].length);
# ^ ~
# 120| if (buffer_type & SIMPLETLV_VALUE) {
# 121| if (tlv[i].type == SIMPLETLV_TYPE_LEAF) {
# 122|-> memcpy(p, tlv[i].value.value, tlv[i].length);
# 123| p += tlv[i].length;
# 124| } else {
I think there is a corner case when nested simpletlv_encode_internal()
returns zero, but I do not think this happens in the current code.