libdvbv5: avoid playing with memory
authorMauro Carvalho Chehab <m.chehab@samsung.com>
Sat, 16 Nov 2013 10:56:08 +0000 (08:56 -0200)
committerMauro Carvalho Chehab <m.chehab@samsung.com>
Wed, 27 Nov 2013 11:24:40 +0000 (09:24 -0200)
Instead of reallocating the memory size for every single table,
just use the known size of the structure, as this is fixed.

The pat table here is an special case, due to the way its struct
is mapped. It is probably a good idea to use malloc there for the
struct dvb_table_pat_program program, but this would break compatibility
with libdvbv5 apps. So, let's keep it as-is.

Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
lib/include/descriptors.h
lib/libdvbv5/descriptors.c
lib/libdvbv5/dvb-scan.c

index da05091..b042ce3 100644 (file)
@@ -39,6 +39,7 @@ typedef void (*dvb_table_init_func)(struct dvb_v5_fe_parms *parms, const uint8_t
 
 struct dvb_table_init {
        dvb_table_init_func init;
+       ssize_t size;
 };
 
 extern const struct dvb_table_init dvb_table_initializers[];
index 875fceb..fc1cdac 100644 (file)
@@ -74,14 +74,14 @@ void dvb_desc_default_print(struct dvb_v5_fe_parms *parms, const struct dvb_desc
 }
 
 const struct dvb_table_init dvb_table_initializers[] = {
-       [DVB_TABLE_PAT] = { dvb_table_pat_init },
-       [DVB_TABLE_PMT] = { dvb_table_pmt_init },
-       [DVB_TABLE_NIT] = { dvb_table_nit_init },
-       [DVB_TABLE_SDT] = { dvb_table_sdt_init },
-       [DVB_TABLE_EIT] = { dvb_table_eit_init },
-       [DVB_TABLE_TVCT] = { dvb_table_vct_init },
-       [DVB_TABLE_CVCT] = { dvb_table_vct_init },
-       [DVB_TABLE_EIT_SCHEDULE] = { dvb_table_eit_init },
+       [DVB_TABLE_PAT] = { dvb_table_pat_init, 0 /* Size here is variable */ },
+       [DVB_TABLE_PMT] = { dvb_table_pmt_init, sizeof(struct dvb_table_pmt) },
+       [DVB_TABLE_NIT] = { dvb_table_nit_init, sizeof(struct dvb_table_nit) },
+       [DVB_TABLE_SDT] = { dvb_table_sdt_init, sizeof(struct dvb_table_sdt) },
+       [DVB_TABLE_EIT] = { dvb_table_eit_init, sizeof(struct dvb_table_eit) },
+       [DVB_TABLE_TVCT] = { dvb_table_vct_init, sizeof(struct dvb_table_vct) },
+       [DVB_TABLE_CVCT] = { dvb_table_vct_init, sizeof(struct dvb_table_vct) },
+       [DVB_TABLE_EIT_SCHEDULE] = { dvb_table_eit_init, sizeof(struct dvb_table_eit) },
 };
 
 char *default_charset = "iso-8859-1";
index a048376..ff153b0 100644 (file)
@@ -185,18 +185,21 @@ int dvb_read_section_with_id(struct dvb_v5_fe_parms *parms, int dmx_fd,
                if (last_section == -1)
                        last_section = h->last_section;
 
-               //ARRAY_SIZE(vb_table_initializers) >= table
                if (!tbl) {
-                       tbl = malloc(MAX_TABLE_SIZE);
+                       if (dvb_table_initializers[tid].size)
+                               tbl = malloc(dvb_table_initializers[tid].size);
+                       else
+                               tbl = malloc(MAX_TABLE_SIZE);
                        if (!tbl)
                                dvb_perror("Out of memory");
                }
 
                if (dvb_table_initializers[tid].init) {
                        dvb_table_initializers[tid].init(parms, buf, buf_length, tbl, &table_length);
-                       tbl = realloc(tbl, table_length);
                        if (!tbl)
                                dvb_perror("Out of memory");
+                       if (!dvb_table_initializers[tid].size)
+                               tbl = realloc(tbl, table_length);
                } else
                        dvb_logerr("dvb_read_section: no initializer for table %d", tid);