Skip to content

Commit

Permalink
null alloc check
Browse files Browse the repository at this point in the history
Signed-off-by: junka <wan.junjie@foxmail.com>
  • Loading branch information
junka committed Jul 27, 2024
1 parent 0ee6cb8 commit 00b3c41
Show file tree
Hide file tree
Showing 23 changed files with 194 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Makefile.in
Makefile
.vscode/
.DS_Store
.vs/
11 changes: 4 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src SRC_LIST)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)

# SET(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang")
# SET(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang++")

IF (WIN32)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
ELSE()
Expand All @@ -31,8 +28,8 @@ ENDIF()

ADD_EXECUTABLE(tsanalyze ${SRC_LIST})

#find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
#find_package(pybind11 REQUIRED)
#python3 -m pybind11 --includes
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
# on windows, use vcpkg install pybind11 and configure PYBIND11_DIR manually
find_package(pybind11 CONFIG REQUIRED)

#pybind11_add_module(tsana ./pybind/binding.cpp ${SRC_LIST})
pybind11_add_module(tsana ./pybind/binding.cpp ${SRC_LIST})
6 changes: 3 additions & 3 deletions include/comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
#define container_of(item, type, member) ((type *)((char *)item - (char *)(&((type *)0)->member)))
#endif

//For msvc cl.exe see https://learn.microsoft.com/zh-cn/cpp/c-language/typeof-c?view=msvc-170
//For MSVC cl.exe see https://learn.microsoft.com/zh-cn/cpp/c-language/typeof-c?view=msvc-170
// see https://learn.microsoft.com/zh-cn/cpp/overview/compiler-versions?view=msvc-170

#if _MSC_VER >= 1939
Expand All @@ -48,12 +48,12 @@ extern "C" {

#define BUILD_ASSERT_OR_ZERO(cond) (sizeof(char[1 - 2 * !(cond)]) - 1)

#if HAVE_TYPEOF
#ifdef HAVE_TYPEOF
#define check_type(expr, type) ((typeof(expr) *)0 != (type *)0)

#define check_types_match(expr1, expr2) ((typeof(expr1) *)0 != (typeof(expr2) *)0)
#else
/* Without typeof, we can only test the sizes. */
/* Without 'typeof', we can only test the sizes. */
#define check_type(expr, type) BUILD_ASSERT_OR_ZERO(sizeof(expr) == sizeof(type))

#define check_types_match(expr1, expr2) BUILD_ASSERT_OR_ZERO(sizeof(expr1) == sizeof(expr2))
Expand Down
12 changes: 8 additions & 4 deletions include/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ struct MuxCodeSlot
uint8_t numberOfBytes;
} PACK;

struct MuxCodeSubstrue
struct MuxCodeSubstruct
{
uint8_t slotCount : 5;
uint8_t repetitionCount : 3;
Expand All @@ -312,7 +312,7 @@ struct MuxCodeTableEntry
uint8_t MuxCode : 4;
uint8_t version : 4;
uint8_t substructureCount;
struct MuxCodeSubstrue substructure;
struct MuxCodeSubstruct substructure;
} PACK;

#define foreach_muxcode_member \
Expand Down Expand Up @@ -478,6 +478,7 @@ foreach_enum_descriptor
dr->name = NULL; \
while(dr->descriptor.length + 2 - bytes_off > 0) { \
dr->name = (type *)realloc(dr->name, (dr->name##_cnt + 1) * sizeof(type)); \
if (!dr->name) { return ENOMEM; } \
memcpy(dr->name + dr->name##_cnt, buf + bytes_off, sizeof(type)); \
dr->name##_cnt ++; \
bytes_off += sizeof(type) ; \
Expand All @@ -490,6 +491,7 @@ foreach_enum_descriptor
dr->name = NULL; \
while (dr->descriptor.length + 2 - bytes_off > 0) { \
dr->name = (type *) realloc(dr->name, ((dr->name##_cnt + 1) * sizeof(type))); \
if (!dr->name) { return ENOMEM; } \
bytes_off += parse_cb(buf + bytes_off, dr->descriptor.length- bytes_off, &dr->name[dr->name##_cnt]); \
dr->name##_cnt ++; \
}
Expand Down Expand Up @@ -517,6 +519,7 @@ foreach_enum_descriptor
} else { \
dr->name = (type *)calloc(dr->length, sizeof(type)); \
} \
if (!dr->name) { return ENOMEM; } \
for (uint32_t i_ = 0; i_ < dr->length; i_++) { \
*(dr->name + i_) = *(type *)(buf + bytes_off); \
bytes_off += sizeof(type); \
Expand All @@ -531,6 +534,7 @@ foreach_enum_descriptor
while(len - bytes_off) { \
dr->name##_num ++; \
dr->name = (type *)realloc(dr->name, sizeof(type) * dr->name##_num); \
if (!dr->name) { return ENOMEM; } \
memcpy(dr->name + dr->name##_num - 1, buf + bytes_off, offsetof(type, length)); \
bytes_off += offsetof(type, length); \
dr->name[dr->name##_num - 1].length = TS_READ8(buf + bytes_off); \
Expand All @@ -539,6 +543,7 @@ foreach_enum_descriptor
v += offsetof(type, length) + sizeof(dr->name[dr->name##_num - 1].length); \
uintptr_t **vv = (uintptr_t **)v; \
*vv = (uintptr_t *) calloc(1, dr->name[dr->name##_num - 1].length); \
if (!*vv) { return ENOMEM; } \
memcpy(*vv, buf + bytes_off, dr->name[dr->name##_num - 1].length); \
bytes_off += dr->name[dr->name##_num - 1].length; \
}
Expand All @@ -549,6 +554,7 @@ foreach_enum_descriptor
while(len > bytes_off) { \
dr->name##_num ++; \
dr->name = (type *)realloc(dr->name, sizeof(type) * dr->name##_num); \
if (!dr->name) { return ENOMEM; } \
bytes_off += parse_cb(buf + bytes_off, len - bytes_off, dr->name + dr->name##_num - 1); \
}

Expand Down Expand Up @@ -698,8 +704,6 @@ foreach_enum_descriptor
#undef __m1
#undef __m

int parse_tlv(uint8_t *buf);

void init_descriptor_parsers(void);

void free_descriptors(struct list_head *list);
Expand Down
1 change: 1 addition & 0 deletions include/dvb/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ __parse_extend_event_linkage_info(uint8_t *buf, int len, struct extend_event_lin
int l = 0, i = 0;
while (l < e->loop_length) {
e->subinfo = (struct extend_event_linkage_subinfo *)realloc(e->subinfo , (i+1) * sizeof(struct extend_event_linkage_subinfo));
if (!e->subinfo) {return ENOMEM; }
e->subinfo[i].target_event_id = TS_READ16(ptr);
ptr += 2;
l += 2;
Expand Down
2 changes: 1 addition & 1 deletion include/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef struct filter_param
uint8_t depth;
uint8_t coff[MAX_FILTER_DEPTH];
uint8_t mask[MAX_FILTER_DEPTH];
uint8_t negete[MAX_FILTER_DEPTH];
uint8_t negate[MAX_FILTER_DEPTH];
} filter_param_t;

typedef int (*filter_cb)(uint16_t pid, uint8_t *data, uint16_t len);
Expand Down
6 changes: 6 additions & 0 deletions include/isdb/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ __parse_module_info_list(uint8_t *buf, int len, struct module_info_list *m)
m->num_of_modules = TS_READ16(ptr);
ptr += 2;
m->info = (struct module_info *)calloc(m->num_of_modules, sizeof(struct module_info));
if (!m->info) {
return ENOMEM;
}
for (int i = 0; i < m->num_of_modules; i ++) {
m->info[i].module_id = TS_READ16(ptr);
ptr += 2;
Expand Down Expand Up @@ -333,6 +336,9 @@ __parse_text_info(uint8_t *buf, int len, struct text_info *t)
ptr += 4;

t->text_char = (uint8_t *)calloc(1, t->text_length);
if (!t->text_char) {
return ENOMEM;
}
for (int i = 0; i < t->text_length; i++) {
t->text_char[i] = TS_READ8(ptr);
ptr += 1;
Expand Down
2 changes: 1 addition & 1 deletion include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ static inline struct list_node *list_node_from_off_(void *ptr, size_t off)

#define list_off_var_(var, member) (container_off_var(var, member) + check_type(var->member, struct list_node))

#if HAVE_TYPEOF
#ifdef HAVE_TYPEOF
#define list_typeof(var) typeof(var)
#else
#define list_typeof(var) void *
Expand Down
2 changes: 0 additions & 2 deletions include/pes.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ extern "C" {
#include "types.h"
#include "table.h"


#if 0
enum PES_scrambling_control{
PES_non_scrambled = 0x00,
Expand Down Expand Up @@ -59,7 +58,6 @@ typedef struct

typedef struct
{
EXT_STD_C11
union
{
struct
Expand Down
6 changes: 3 additions & 3 deletions include/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ typedef struct

uint16_t reserved2 : 4;
uint16_t bouquet_descriptors_length : 12;
struct list_head list; /*bouquet desriptor list */
struct list_head list; /*bouquet descriptor list */
uint16_t reserved3 : 4;
uint16_t transport_stream_loop_length : 12;
struct list_head h;
Expand Down Expand Up @@ -370,7 +370,7 @@ struct running_status
uint16_t service_id;
uint16_t event_id;
uint8_t reserved_future_use : 5;
uint8_t running_status : 3;
uint8_t running_status_ : 3;
struct running_status *next;
};

Expand Down Expand Up @@ -672,7 +672,7 @@ static inline char const *get_stream_type(uint8_t type)
"ISO/IEC 13818-6 type B",
"ISO/IEC 13818-6 type C",
"ISO/IEC 13818-6 type D",
"ISO/IEC 13818-1 auxillary",
"ISO/IEC 13818-1 auxiliary",
"ISO/IEC 13818-7 Audio with ADTS transport syntax",
"ISO/IEC 14496-2 Visual",
"ISO/IEC 14496-3 Audio with the LATM transport syntax",
Expand Down
6 changes: 4 additions & 2 deletions include/ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern "C" {

#define TS_SYNC_BYTE (0x47)

#pragma pack(push, 1)
typedef struct
{
uint8_t sync_byte; /*0x47*/
Expand All @@ -28,7 +29,8 @@ typedef struct
uint8_t transport_scrambling_control : 2;
uint8_t adaptation_field_control : 2;
uint8_t continuity_counter : 4;
} PACK ts_header;
} ts_header;
#pragma pack(pop)

enum adaptation_field_e {
ADAPT_RESERVED = 0,
Expand Down Expand Up @@ -157,8 +159,8 @@ struct tsa_config
uint8_t detail : 1;
uint8_t stats : 1;
uint8_t mem;
uint16_t tables;
uint8_t output;
uint16_t tables;
};

struct tsa_config *get_config(void);
Expand Down
1 change: 1 addition & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdint.h>
#include "list.h"


#ifdef __cplusplus
extern "C" {
#endif
Expand Down
6 changes: 3 additions & 3 deletions pybind/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace py = pybind11;

char** convertPythonListToArgv(const py::list& args_list) {
static char** convertPythonListToArgv(const py::list& args_list) {
size_t argc = args_list.size();
if (argc == 0) {
return nullptr;
Expand All @@ -25,7 +25,7 @@ char** convertPythonListToArgv(const py::list& args_list) {
return argv;
}

int parse_args(const py::list& args)
static int parse_args(const py::list& args)
{
int argc = static_cast<int>(args.size());
char ** argv = convertPythonListToArgv(args);
Expand Down Expand Up @@ -70,7 +70,7 @@ void deinit()

PYBIND11_MODULE(tsana, m) {
m.doc() = "pybind11 tsanalyze";
m.def("init", &init, "Init module processer");
m.def("init", &init);
m.def("run", &run, "run the parser");
m.def("result", &result, "show parser results");
m.def("deinit", &deinit, "Deinit module and free");
Expand Down
7 changes: 5 additions & 2 deletions src/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ filter_t *filter_alloc(uint16_t pid)
return NULL;

struct filter_slot *fs = (struct filter_slot *)calloc(1, sizeof(struct filter_slot));
if (!fs) {
return ENOMEM;
}
fs->t.pid = pid;
list_add(&pid_filter[pid].h, &(fs->n));
pid_filter[pid].filter_num++;
Expand All @@ -53,7 +56,7 @@ int filter_set(filter_t *f, filter_param_t *p, filter_cb func)
f->para.depth = p->depth;
memcpy(f->para.coff, p->coff, p->depth * sizeof(uint8_t));
memcpy(f->para.mask, p->mask, p->depth * sizeof(uint8_t));
memcpy(f->para.negete, p->negete, p->depth * sizeof(uint8_t));
memcpy(f->para.negate, p->negate, p->depth * sizeof(uint8_t));
}
f->callback = func;
return 0;
Expand Down Expand Up @@ -93,7 +96,7 @@ filter_t *filter_lookup(uint16_t pid, filter_param_t *para)
if (ix->t.para.depth == para->depth) {
if (0 == memcmp(ix->t.para.coff, para->coff, para->depth * sizeof(uint8_t)) &&
0 == memcmp(ix->t.para.mask, para->mask, para->depth * sizeof(uint8_t)) &&
0 == memcmp(ix->t.para.negete, para->negete, para->depth * sizeof(uint8_t)))
0 == memcmp(ix->t.para.negate, para->negate, para->depth * sizeof(uint8_t)))
{
f = &ix->t;
break;
Expand Down
4 changes: 4 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void dump_result(int sig)

int main(int argc, char *argv[])
{
#if __STDC_VERSION__ < 201112L
printf("good");
exit(0);
#endif
fileio_init();
udp_io_init();
int ret;
Expand Down
4 changes: 2 additions & 2 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ static void prog_usage(FILE *fp, const char *pro_name)
{
if (fp == NULL)
fp = stderr;
fprintf(fp, "Usage: %s [optins]... <file>\n", pro_name);
fprintf(fp, " Display infomations about mpeg ts.\n\n");
fprintf(fp, "Usage: %s [options]... <file>\n", pro_name);
fprintf(fp, " Display informations about mpeg ts.\n\n");
fprintf(fp, "%13s%c%s\t%s\n", " -", OPT_HELP_NUM, ", --" OPT_HELP, "\tShow this help");
fprintf(fp, "%13s%c%s\t%s\n", " -", OPT_FORMAT_NUM, ", --" OPT_FORMAT, "Select input format [udp][file]");
fprintf(fp, "%13s%c%s\t%s\n", " -", OPT_BRIEF_LIST_NUM, ", --" OPT_BRIEF_LIST, "\tShow all infos in brief");
Expand Down
5 changes: 4 additions & 1 deletion src/pes.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void *pes_private_alloc(uint8_t tag)
{
if (tag == 0x59) {
struct subtitle_pes_data *sub = calloc(1, sizeof(struct subtitle_pes_data));
if (!sub) {
return NULL;
}
list_head_init(&sub->seg_list);
return sub;
} else if (tag == 0x56) {
Expand Down Expand Up @@ -311,7 +314,7 @@ void unregister_pes_ops(void)
.depth = 1,
.coff = {0},
.mask = {0},
.negete = {0},
.negate = {0},
};
filter_t *f = NULL;
for (int i = 0; i < pes.pid_num; i ++) {
Expand Down
6 changes: 6 additions & 0 deletions src/ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ int parse_program_stream_map(uint8_t *pkt, uint16_t len, ps_map *map)
list_head_init(&map->h);
while(k < map->elementary_stream_map_length) {
es_map * node = calloc(1, sizeof(es_map));
if (!node) {
return ENOMEM;
}
list_head_init(&node->list);
list_node_init(&node->n);
node->stream_type = TS_READ8(buf);
Expand Down Expand Up @@ -175,6 +178,9 @@ int parse_directory_PES_packet(uint8_t *pkt, uint16_t len, directory_PES_packet
dpp->marker_bit6 = TS_READ16_BITS(buf, 1, 15);
PL_STEP(buf, l, 2);
dpp->units = calloc(dpp->number_of_access_units, sizeof(access_unit));
if (!dpp->units) {
return ENOMEM;
}
int i = 0;
while(i < dpp->number_of_access_units) {
dpp->units[i].packet_stream_id = TS_READ8(buf);
Expand Down
Loading

0 comments on commit 00b3c41

Please sign in to comment.