Bug Summary

File:src/lib/libcbor/src/cbor/bytestrings.c
Warning:line 49, 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 bytestrings.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/bytestrings.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 "bytestrings.h"
9#include <string.h>
10#include "internal/memory_utils.h"
11
12size_t cbor_bytestring_length(const cbor_item_t *item) {
13 assert(cbor_isa_bytestring(item))((cbor_isa_bytestring(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/bytestrings.c"
, 13, __func__, "cbor_isa_bytestring(item)"))
;
14 return item->metadata.bytestring_metadata.length;
15}
16
17unsigned char *cbor_bytestring_handle(const cbor_item_t *item) {
18 assert(cbor_isa_bytestring(item))((cbor_isa_bytestring(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/bytestrings.c"
, 18, __func__, "cbor_isa_bytestring(item)"))
;
19 return item->data;
20}
21
22bool_Bool cbor_bytestring_is_definite(const cbor_item_t *item) {
23 assert(cbor_isa_bytestring(item))((cbor_isa_bytestring(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/bytestrings.c"
, 23, __func__, "cbor_isa_bytestring(item)"))
;
24 return item->metadata.bytestring_metadata.type == _CBOR_METADATA_DEFINITE;
25}
26
27bool_Bool cbor_bytestring_is_indefinite(const cbor_item_t *item) {
28 return !cbor_bytestring_is_definite(item);
29}
30
31cbor_item_t *cbor_new_definite_bytestring() {
32 cbor_item_t *item = _CBOR_MALLOCmalloc(sizeof(cbor_item_t));
33 _CBOR_NOTNULL(item)do { if (item == ((void*)0)) { return ((void*)0); } } while (
0)
;
34 *item = (cbor_item_t){
35 .refcount = 1,
36 .type = CBOR_TYPE_BYTESTRING,
37 .metadata = {.bytestring_metadata = {_CBOR_METADATA_DEFINITE, 0}}};
38 return item;
39}
40
41cbor_item_t *cbor_new_indefinite_bytestring() {
42 cbor_item_t *item = _CBOR_MALLOCmalloc(sizeof(cbor_item_t));
43 _CBOR_NOTNULL(item)do { if (item == ((void*)0)) { return ((void*)0); } } while (
0)
;
44 *item = (cbor_item_t){
45 .refcount = 1,
46 .type = CBOR_TYPE_BYTESTRING,
47 .metadata = {.bytestring_metadata = {.type = _CBOR_METADATA_INDEFINITE,
48 .length = 0}},
49 .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'
50 _CBOR_DEPENDENT_NOTNULL(item, item->data)do { if (item->data == ((void*)0)) { free(item); return ((
void*)0); } } while (0)
;
51 *((struct cbor_indefinite_string_data *)item->data) =
52 (struct cbor_indefinite_string_data){
53 .chunk_count = 0,
54 .chunk_capacity = 0,
55 .chunks = NULL((void*)0),
56 };
57 return item;
58}
59
60cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length) {
61 cbor_item_t *item = cbor_new_definite_bytestring();
62 _CBOR_NOTNULL(item)do { if (item == ((void*)0)) { return ((void*)0); } } while (
0)
;
63 void *content = _CBOR_MALLOCmalloc(length);
64 _CBOR_DEPENDENT_NOTNULL(item, content)do { if (content == ((void*)0)) { free(item); return ((void*)
0); } } while (0)
;
65 memcpy(content, handle, length);
66 cbor_bytestring_set_handle(item, content, length);
67 return item;
68}
69
70void cbor_bytestring_set_handle(cbor_item_t *item,
71 cbor_mutable_data CBOR_RESTRICT_POINTERrestrict data,
72 size_t length) {
73 assert(cbor_isa_bytestring(item))((cbor_isa_bytestring(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/bytestrings.c"
, 73, __func__, "cbor_isa_bytestring(item)"))
;
74 assert(cbor_bytestring_is_definite(item))((cbor_bytestring_is_definite(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/bytestrings.c"
, 74, __func__, "cbor_bytestring_is_definite(item)"))
;
75 item->data = data;
76 item->metadata.bytestring_metadata.length = length;
77}
78
79cbor_item_t **cbor_bytestring_chunks_handle(const cbor_item_t *item) {
80 assert(cbor_isa_bytestring(item))((cbor_isa_bytestring(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/bytestrings.c"
, 80, __func__, "cbor_isa_bytestring(item)"))
;
81 assert(cbor_bytestring_is_indefinite(item))((cbor_bytestring_is_indefinite(item)) ? (void)0 : __assert2(
"/usr/src/lib/libcbor/src/cbor/bytestrings.c", 81, __func__, "cbor_bytestring_is_indefinite(item)"
))
;
82 return ((struct cbor_indefinite_string_data *)item->data)->chunks;
83}
84
85size_t cbor_bytestring_chunk_count(const cbor_item_t *item) {
86 assert(cbor_isa_bytestring(item))((cbor_isa_bytestring(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/bytestrings.c"
, 86, __func__, "cbor_isa_bytestring(item)"))
;
87 assert(cbor_bytestring_is_indefinite(item))((cbor_bytestring_is_indefinite(item)) ? (void)0 : __assert2(
"/usr/src/lib/libcbor/src/cbor/bytestrings.c", 87, __func__, "cbor_bytestring_is_indefinite(item)"
))
;
88 return ((struct cbor_indefinite_string_data *)item->data)->chunk_count;
89}
90
91bool_Bool cbor_bytestring_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {
92 assert(cbor_isa_bytestring(item))((cbor_isa_bytestring(item)) ? (void)0 : __assert2("/usr/src/lib/libcbor/src/cbor/bytestrings.c"
, 92, __func__, "cbor_isa_bytestring(item)"))
;
93 assert(cbor_bytestring_is_indefinite(item))((cbor_bytestring_is_indefinite(item)) ? (void)0 : __assert2(
"/usr/src/lib/libcbor/src/cbor/bytestrings.c", 93, __func__, "cbor_bytestring_is_indefinite(item)"
))
;
94 struct cbor_indefinite_string_data *data =
95 (struct cbor_indefinite_string_data *)item->data;
96 if (data->chunk_count == data->chunk_capacity) {
97 // TODO: Add a test for this
98 if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH2, data->chunk_capacity)) {
99 return false0;
100 }
101
102 size_t new_chunk_capacity =
103 data->chunk_capacity == 0 ? 1
104 : CBOR_BUFFER_GROWTH2 * (data->chunk_capacity);
105
106 cbor_item_t **new_chunks_data = _cbor_realloc_multiple(
107 data->chunks, sizeof(cbor_item_t *), new_chunk_capacity);
108
109 if (new_chunks_data == NULL((void*)0)) {
110 return false0;
111 }
112 data->chunk_capacity = new_chunk_capacity;
113 data->chunks = new_chunks_data;
114 }
115 data->chunks[data->chunk_count++] = cbor_incref(chunk);
116 return true1;
117}