Hi Philipp,

> This causes a SEGV because the progtbl listing through printf does not check 
> for NULL pointers when accessing strings:

maybe I'm not looking right. But which string null pointer do you mean? We are 
talking about vbi_dvb_open(), right?


>         for (i = 0; i < progcnt; i++) {
>                 printf("Service ID 0x%04x Type 0x%02x Provider Name \"%s\" 
> Name \"%s\"\n"
>                        "  PMT PID 0x%04x TXT: PID 0x%04x lang %.3s type 
> 0x%02x magazine %1u page %3u\n",
>                        progtbl[i].service_id, progtbl[i].service_type, 
> progtbl[i].service_provider_name,
>                        progtbl[i].service_name, progtbl[i].pmtpid, 
> progtbl[i].txtpid, progtbl[i].txtlang,
>                        progtbl[i].txttype, progtbl[i].txtmagazine, 
> progtbl[i].txtpage);
>         }

There are three structure elements printf tries to print as a string: 
progtbl[i].service_provider_name, progtbl[i].service_name nad 
progtbl[i].txtlang. All of them are stack allocated arrays, completly 
initialized by \0 with memset() by default.


> $3 = {pmtpid = 0, txtpid = 0, service_id = 0, service_type = 0 '\0', 
> service_provider_name = '\0' <repeats 63 times>, service_name = '\0' <repeats 
> 63 times>, txtlang = "\0\0", txttype = 0 '\0', 
>   txtmagazine = 0 '\0', txtpage = 0 '\0'}

This debug output shows that service_provider_name, service_name and txtlang 
are _containing_ zeroes.


I attach an easy to compile and run excerpt of vbi_dvb_open(). It contains only 
the code of definition, initialization and output of progtbl. All 16 structure 
elements are completly filled with zeroes. The output works without any 
segmentation fault.

Am I wrong?

Would you please help me in understanding the problem?

Could you reproduce the segmentation fault?
Are you sure this is the exact position where you got the segmentation fault? 
What do I have to do to reproduce the segmentation fault?


regards Dirk
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char **argv)
{
	struct {
		u_int16_t pmtpid;
		u_int16_t txtpid;
		u_int16_t service_id;
		u_int8_t service_type;
		char service_provider_name[64];
		char service_name[64];
		u_int8_t txtlang[3];
		u_int8_t txttype;
		u_int8_t txtmagazine;
		u_int8_t txtpage;
	} progtbl[16];

	unsigned int i, progcnt = 0;

	memset(progtbl, 0, sizeof(progtbl));
	progcnt = sizeof(progtbl)/sizeof(progtbl[0]);

	for (i = 0; i < progcnt; i++) {
		printf("Service ID 0x%04x Type 0x%02x Provider Name \"%s\" Name \"%s\"\n"
		       "  PMT PID 0x%04x TXT: PID 0x%04x lang %.3s type 0x%02x magazine %1u page %3u\n",
		       progtbl[i].service_id, progtbl[i].service_type, progtbl[i].service_provider_name,
		       progtbl[i].service_name, progtbl[i].pmtpid, progtbl[i].txtpid, progtbl[i].txtlang,
		       progtbl[i].txttype, progtbl[i].txtmagazine, progtbl[i].txtpage);
	}

	return 0;
}

Reply via email to