Mega Newbie pointer question

B

Beginner

Hi,

I am trying to use the LIBTIFF library to read some metadata (IPTC)
stored in the file. The library provides a simply interface to the data
via a TAG reference . My problem is that I am sooo green with C that I
am not sure how to handle the data. Below is what I have written so
far. It compiles (Linux GCC 3.4.2), runs and outputs the text I expect
but then it seg faults.

I have no real sanity checks on because I don't know how to convert the
data in the pointer into character text. Can anyone offer any advice on
how to de-reference the data correctly (cast hasn't worked for me
either) and possibly some way to check that the data coming through is
of the type char?

TIA.


=================
include <stdio.h>
include <tiffio.h>


main()

int i, j;
uint16 count;
void *data;
char iptc_data[600];

TIFF* tif = TIFFOpen("test.tif", "r");
if (tif == NULL) {
printf("Failed to open file\n");
exit(8);
}
if (TIFFGetField(tif, TIFFTAG_RICHTIFFIPTC, &count, &data) == 0) {
printf("No IPTC Tag found\n");
exit(8);
}

printf("Size of IPTC Tag is %d bytes. %s.\n", count , &data);

for ( i = 0; i < count; ++i) {
strcat(iptc_data, &data);
}

printf("%s\n", iptc_data);
TIFFClose(tif);
return 0;
}

=====================
 
M

Mike Wahler

Beginner said:
Hi,

I am trying to use the LIBTIFF library to read some metadata (IPTC)
stored in the file. The library provides a simply interface to the data
via a TAG reference . My problem is that I am sooo green with C that I
am not sure how to handle the data. Below is what I have written so
far. It compiles (Linux GCC 3.4.2), runs and outputs the text I expect
but then it seg faults.

I have no real sanity checks on because I don't know how to convert the
data in the pointer into character text. Can anyone offer any advice on
how to de-reference the data correctly (cast hasn't worked for me
either) and possibly some way to check that the data coming through is
of the type char?

TIA.


=================
include <stdio.h>
include <tiffio.h>

LIBTIFF is some third-party library which makes it
off-topic here (and you can't know if any advice you
might get about it is accurate). However, I'll still
guess at what's wrong. See below.
main()

int i, j;
uint16 count;
void *data;

This defines storage for a single pointer object. This
object can store a single address. At this point, this
pointer does not represent any address at all (its value
is indeterminate, unknown. Thus, any attempt to access
its value or to dereference it will produce 'undefined'
behavior' (which can manifest itself in an unlimited
number of ways, one of which could be a 'seg fault'.
char iptc_data[600];

TIFF* tif = TIFFOpen("test.tif", "r");
if (tif == NULL) {
printf("Failed to open file\n");
exit(8);
}
if (TIFFGetField(tif, TIFFTAG_RICHTIFFIPTC, &count, &data) == 0) {
printf("No IPTC Tag found\n");
exit(8);

I'll guess that the nonstandard function 'TIFFGetField()' function
stores data at the address stored in the pointer 'data'. But this
pointer does NOT contain the address of any memory 'owned' by your
program.

You need to make 'data' point to a memory area for the function
to use. Either define or allocate some memory, and assign its
address to 'data'. E.g.:

void *data;

/* etc */

data = malloc(some_number_of_bytes);

/* check malloc() call for success/failure here */

/* then call 'TIFFGetField()' */

If you need assistance with the LIBTIFF library itself, consult
support resources for it (e.g. mailing lists, newsgroup, vendor
web site, etc.. We only discuss the ISO standard C language here.

-Mike
 
C

Chris Torek

LIBTIFF is some third-party library which makes it
off-topic here (and you can't know if any advice you
might get about it is accurate).
Indeed.

However, I'll still guess at what's wrong. See below.

I think your guess is wrong :)
This defines storage for a single pointer object. This
object can store a single address. ...

I'll guess that the nonstandard function 'TIFFGetField()' function
stores data at the address stored in the pointer 'data'.

I think this is not the problem. (I believe that TIFFGetField does
its own malloc() and sets *vpp to the result.)

Depending on which version of libtiff one has, the "count" provided
via &count here must be a "uint32". Probably the code is writing
32 bits' worth of data into a 16-bit container, and clobbering some
unrelated variable in the process.

Perhaps equally importantly, in the original code, "Beginner" went
on to use strcat() with inappropriate parameters. In particular,
the target buffer for strcat() is not necessarily initially empty,
and the source values are not necessarily strings (and certainly
have not had their data-types converted correctly). (While the
"count" filled in is a count of 4-byte values, it is not really
clear what those values are, given the documentation.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top