Bug Summary

File:src/lib/libcbor/src/cbor/strings.c
Warning:line 30, column 15
Result of 'malloc' is converted to a pointer of type 'unsigned char', which is incompatible with sizeof operand type 'struct cbor_indefinite_string_data'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name strings.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/lib/libcbor/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/lib/libcbor/src -D HAVE_ENDIAN_H -D PIC -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wno-missing-field-initializers -std=c99 -fdebug-compilation-dir=/usr/src/lib/libcbor/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/lib/libcbor/src/cbor/strings.c
1/*
2 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3 *
4 * libcbor is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
6 */
7
8#include "strings.h"
9#include <string.h>
10#include "internal/memory_utils.h"
11
12cbor_item_t *cbor_new_definite_string() {
13 cbor_item_t *item = _CBOR_MALLOCmalloc(sizeof(cbor_item_t));
14 _CBOR_NOTNULL(item)do { if (item == ((void*)0)) { return ((void*)0); } } while (
0)
;
15 *item = (cbor_item_t){
16 .refcount = 1,
17 .type = CBOR_TYPE_STRING,
18 .metadata = {.string_metadata = {_CBOR_METADATA_DEFINITE, 0}}};
19 return item;
20}
21
22cbor_item_t *cbor_new_indefinite_string() {
23 cbor_item_t *item = _CBOR_MALLOCmalloc(sizeof(cbor_item_t));
24 _CBOR_NOTNULL(item)do { if (item == ((void*)0)) { return ((void*)0); } } while (
0)
;
25 *item = (cbor_item_t){
26 .refcount = 1,
27 .type = CBOR_TYPE_STRING,
28 .metadata = {.string_metadata = {.type = _CBOR_METADATA_INDEFINITE,
29 .length = 0}},
30 .data = _CBOR_MALLOCmalloc(sizeof(struct cbor_indefinite_string_data))};
Result of 'malloc' is converted to a pointer of type 'unsigned char', which is incompatible with sizeof operand type 'struct cbor_indefinite_string_data'
31 _CBOR_DEPENDENT_NOTNULL(item, item->data)do { if (item->data == ((void*)0)) { free(item); return ((
void*)0); } } while (0)
;
32 *((struct cbor_indefinite_string_data *)item->data) =
33 (struct cbor_indefinite_string_data){
34 .chunk_count = 0,
35 .chunk_capacity = 0,
36 .chunks = NULL((void*)0),
37 };
38 return item;
39}
40
41cbor_item_t *cbor_build_string(const char *val) {
42 cbor_item_t *item = cbor_new_definite_string();
43 _CBOR_NOTNULL(item)do { if (item == ((void*)0)) { return ((void*)0); } } while (
0)
;
44 size_t len = strlen(val);
45 unsigned char *handle = _CBOR_MALLOCmalloc(len);
46 _CBOR_DEPENDENT_NOTNULL(item, handle)do { if (handle == ((void*)0)) { free(item); return ((void*)0
); } } while (0)
;
47 memcpy(handle, val, len);
48 cbor_string_set_handle(item, handle, len);
49 return item;
50}
51
52cbor_item_t *cbor_build_stringn(const char *val, size_t length) {
53 cbor_item_t *item = cbor_new_definite_string();
54 _CBOR_NOTNULL(item)do { if (item == ((void*)0)) { return ((void*)0); } } while (
0)
;
55 unsigned char *handle = _CBOR_MALLOCmalloc(length);
56 _CBOR_DEPENDENT_NOTNULL(item, handle)do { if (handle == ((void*)0)) { free(item); return ((void*)0
); } } while (0)
;
57 memcpy(handle, val, length);
58 cbor_string_set_handle(item, handle, length);
59 return item;
60}
61
62void cbor_string_set_handle(cbor_item_t *item,
63 cbor_mutable_data CBOR_RESTRICT_POINTERrestrict data,
64 size_t length) {
65 assert(cbor_isa_string(item))((cbor_isa_string(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 65, __func__, "cbor_isa_string(item)"))
;
66 assert(cbor_string_is_definite(item))((cbor_string_is_definite(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 66, __func__, "cbor_string_is_definite(item)"))
;
67 item->data = data;
68 item->metadata.string_metadata.length = length;
69}
70
71cbor_item_t **cbor_string_chunks_handle(const cbor_item_t *item) {
72 assert(cbor_isa_string(item))((cbor_isa_string(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 72, __func__, "cbor_isa_string(item)"))
;
73 assert(cbor_string_is_indefinite(item))((cbor_string_is_indefinite(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 73, __func__, "cbor_string_is_indefinite(item)"))
;
74 return ((struct cbor_indefinite_string_data *)item->data)->chunks;
75}
76
77size_t cbor_string_chunk_count(const cbor_item_t *item) {
78 assert(cbor_isa_string(item))((cbor_isa_string(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 78, __func__, "cbor_isa_string(item)"))
;
79 assert(cbor_string_is_indefinite(item))((cbor_string_is_indefinite(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 79, __func__, "cbor_string_is_indefinite(item)"))
;
80 return ((struct cbor_indefinite_string_data *)item->data)->chunk_count;
81}
82
83bool_Bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {
84 assert(cbor_isa_string(item))((cbor_isa_string(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 84, __func__, "cbor_isa_string(item)"))
;
85 assert(cbor_string_is_indefinite(item))((cbor_string_is_indefinite(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 85, __func__, "cbor_string_is_indefinite(item)"))
;
86 struct cbor_indefinite_string_data *data =
87 (struct cbor_indefinite_string_data *)item->data;
88 if (data->chunk_count == data->chunk_capacity) {
89 // TODO: Add a test for this
90 if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH2, data->chunk_capacity)) {
91 return false0;
92 }
93
94 size_t new_chunk_capacity =
95 data->chunk_capacity == 0 ? 1
96 : CBOR_BUFFER_GROWTH2 * (data->chunk_capacity);
97 cbor_item_t **new_chunks_data = _cbor_realloc_multiple(
98 data->chunks, sizeof(cbor_item_t *), new_chunk_capacity);
99
100 if (new_chunks_data == NULL((void*)0)) {
101 return false0;
102 }
103
104 data->chunk_capacity = new_chunk_capacity;
105 data->chunks = new_chunks_data;
106 }
107 data->chunks[data->chunk_count++] = cbor_incref(chunk);
108 return true1;
109}
110
111size_t cbor_string_length(const cbor_item_t *item) {
112 assert(cbor_isa_string(item))((cbor_isa_string(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 112, __func__, "cbor_isa_string(item)"))
;
113 return item->metadata.string_metadata.length;
114}
115
116unsigned char *cbor_string_handle(const cbor_item_t *item) {
117 assert(cbor_isa_string(item))((cbor_isa_string(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 117, __func__, "cbor_isa_string(item)"))
;
118 return item->data;
119}
120
121size_t cbor_string_codepoint_count(const cbor_item_t *item) {
122 assert(cbor_isa_string(item))((cbor_isa_string(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 122, __func__, "cbor_isa_string(item)"))
;
123 return item->metadata.string_metadata.codepoint_count;
124}
125
126bool_Bool cbor_string_is_definite(const cbor_item_t *item) {
127 assert(cbor_isa_string(item))((cbor_isa_string(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/strings.c"
, 127, __func__, "cbor_isa_string(item)"))
;
128 return item->metadata.string_metadata.type == _CBOR_METADATA_DEFINITE;
129}
130
131bool_Bool cbor_string_is_indefinite(const cbor_item_t *item) {
132 return !cbor_string_is_definite(item);
133}