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.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name strings.c -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 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -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/llvm16/lib/clang/16 -I /usr/src/lib/libcbor/src -D HAVE_ENDIAN_H -D _cbor_malloc=malloc -D _cbor_realloc=realloc -D _cbor_free=free -internal-isystem /usr/local/llvm16/lib/clang/16/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 -fcf-protection=branch -fno-jump-tables -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/scan/2024-01-11-140451-98009-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(void) {
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(void) {
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 CBOR_ASSERT(cbor_isa_string(item));
66 CBOR_ASSERT(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 CBOR_ASSERT(cbor_isa_string(item));
73 CBOR_ASSERT(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 CBOR_ASSERT(cbor_isa_string(item));
79 CBOR_ASSERT(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 CBOR_ASSERT(cbor_isa_string(item));
85 CBOR_ASSERT(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 if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH2, data->chunk_capacity)) {
90 return false0;
91 }
92
93 size_t new_chunk_capacity =
94 data->chunk_capacity == 0 ? 1
95 : CBOR_BUFFER_GROWTH2 * (data->chunk_capacity);
96 cbor_item_t **new_chunks_data = _cbor_realloc_multiple(
97 data->chunks, sizeof(cbor_item_t *), new_chunk_capacity);
98
99 if (new_chunks_data == NULL((void *)0)) {
100 return false0;
101 }
102
103 data->chunk_capacity = new_chunk_capacity;
104 data->chunks = new_chunks_data;
105 }
106 data->chunks[data->chunk_count++] = cbor_incref(chunk);
107 return true1;
108}
109
110size_t cbor_string_length(const cbor_item_t *item) {
111 CBOR_ASSERT(cbor_isa_string(item));
112 return item->metadata.string_metadata.length;
113}
114
115unsigned char *cbor_string_handle(const cbor_item_t *item) {
116 CBOR_ASSERT(cbor_isa_string(item));
117 return item->data;
118}
119
120size_t cbor_string_codepoint_count(const cbor_item_t *item) {
121 CBOR_ASSERT(cbor_isa_string(item));
122 return item->metadata.string_metadata.codepoint_count;
123}
124
125bool_Bool cbor_string_is_definite(const cbor_item_t *item) {
126 CBOR_ASSERT(cbor_isa_string(item));
127 return item->metadata.string_metadata.type == _CBOR_METADATA_DEFINITE;
128}
129
130bool_Bool cbor_string_is_indefinite(const cbor_item_t *item) {
131 return !cbor_string_is_definite(item);
132}