Add some protection against opening multiple streams in the same file at the same time.

This commit is contained in:
Michael R Sweet 2022-07-04 13:03:11 -04:00
parent 50f27974cf
commit 067683cbcd
No known key found for this signature in database
GPG Key ID: 999559A027815955
5 changed files with 33 additions and 7 deletions

View File

@ -8,6 +8,8 @@ v1.1.0 (Month DD, YYYY)
- Added `pdfioFileCreateTemporary` function (Issue #29) - Added `pdfioFileCreateTemporary` function (Issue #29)
- Added `pdfioDictIterateKeys` function (Issue #31) - Added `pdfioDictIterateKeys` function (Issue #31)
- Added `pdfioContentPathEnd` function. - Added `pdfioContentPathEnd` function.
- Added protection against opening multiple streams in the same file at the
same time.
- Fixed "install-shared" target (Issue #32) - Fixed "install-shared" target (Issue #32)
- Fixed `pdfioContentMatrixRotate` function. - Fixed `pdfioContentMatrixRotate` function.

View File

@ -1,7 +1,7 @@
// //
// PDF object functions for PDFio. // PDF object functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2022 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -33,8 +33,14 @@ pdfioObjClose(pdfio_obj_t *obj) // I - Object
if (!obj) if (!obj)
return (false); return (false);
// Clear the current object pointer...
obj->pdf->current_obj = NULL;
if (obj->pdf->mode != _PDFIO_MODE_WRITE) if (obj->pdf->mode != _PDFIO_MODE_WRITE)
return (true); // Nothing to do when reading {
// Nothing to do when reading
return (true);
}
// Write what remains for the object... // Write what remains for the object...
if (!obj->offset) if (!obj->offset)
@ -165,6 +171,12 @@ pdfioObjCreateStream(
return (NULL); return (NULL);
} }
if (obj->pdf->current_obj)
{
_pdfioFileError(obj->pdf, "Another object (%u) is already open.", (unsigned)obj->pdf->current_obj->number);
return (NULL);
}
// Write the header... // Write the header...
if (!_pdfioDictGetValue(obj->value.value.dict, "Length")) if (!_pdfioDictGetValue(obj->value.value.dict, "Length"))
{ {
@ -193,7 +205,8 @@ pdfioObjCreateStream(
if (!_pdfioFilePuts(obj->pdf, "stream\n")) if (!_pdfioFilePuts(obj->pdf, "stream\n"))
return (NULL); return (NULL);
obj->stream_offset = _pdfioFileTell(obj->pdf); obj->stream_offset = _pdfioFileTell(obj->pdf);
obj->pdf->current_obj = obj;
// Return the new stream... // Return the new stream...
return (_pdfioStreamCreate(obj, length_obj, filter)); return (_pdfioStreamCreate(obj, length_obj, filter));
@ -454,6 +467,12 @@ pdfioObjOpenStream(pdfio_obj_t *obj, // I - Object
if (!obj) if (!obj)
return (NULL); return (NULL);
if (obj->pdf->current_obj)
{
_pdfioFileError(obj->pdf, "Another object (%u) is already open.", (unsigned)obj->pdf->current_obj->number);
return (NULL);
}
// Make sure we've loaded the object dictionary... // Make sure we've loaded the object dictionary...
if (!obj->value.type) if (!obj->value.type)
{ {
@ -466,6 +485,8 @@ pdfioObjOpenStream(pdfio_obj_t *obj, // I - Object
return (NULL); return (NULL);
// Open the stream... // Open the stream...
obj->pdf->current_obj = obj;
return (_pdfioStreamOpen(obj, decode)); return (_pdfioStreamOpen(obj, decode));
} }

View File

@ -1,7 +1,7 @@
// //
// Private header file for PDFio. // Private header file for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2022 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -289,7 +289,8 @@ struct _pdfio_file_s // PDF file structure
pdfio_dict_t **dicts; // Dictionaries pdfio_dict_t **dicts; // Dictionaries
size_t num_objs, // Number of objects size_t num_objs, // Number of objects
alloc_objs; // Allocated objects alloc_objs; // Allocated objects
pdfio_obj_t **objs; // Objects pdfio_obj_t **objs, // Objects
*current_obj; // Current object being written/read
size_t num_objmaps, // Number of object maps size_t num_objmaps, // Number of object maps
alloc_objmaps; // Allocated object maps alloc_objmaps; // Allocated object maps
_pdfio_objmap_t *objmaps; // Object maps _pdfio_objmap_t *objmaps; // Object maps

View File

@ -174,6 +174,8 @@ pdfioStreamClose(pdfio_stream_t *st) // I - Stream
done: done:
st->pdf->current_obj = NULL;
free(st->prbuffer); free(st->prbuffer);
free(st->psbuffer); free(st->psbuffer);
free(st); free(st);

View File

@ -3239,13 +3239,13 @@ write_unit_file(
// Create some image objects... // Create some image objects...
fputs("pdfioFileCreateImageObjFromFile(\"testfiles/color.jpg\"): ", stdout); fputs("pdfioFileCreateImageObjFromFile(\"testfiles/color.jpg\"): ", stdout);
if ((color_jpg = pdfioFileCreateImageObjFromFile(outpdf, "testfiles/color.jpg", true)) != NULL) if ((color_jpg = pdfioFileCreateImageObjFromFile(outpdf, "testfiles/color.jpg", true)) != NULL)
puts("PASS"); printf("PASS (%u)\n", (unsigned)pdfioObjGetNumber(color_jpg));
else else
return (1); return (1);
fputs("pdfioFileCreateImageObjFromFile(\"testfiles/gray.jpg\"): ", stdout); fputs("pdfioFileCreateImageObjFromFile(\"testfiles/gray.jpg\"): ", stdout);
if ((gray_jpg = pdfioFileCreateImageObjFromFile(outpdf, "testfiles/gray.jpg", true)) != NULL) if ((gray_jpg = pdfioFileCreateImageObjFromFile(outpdf, "testfiles/gray.jpg", true)) != NULL)
puts("PASS"); printf("PASS (%u)\n", (unsigned)pdfioObjGetNumber(gray_jpg));
else else
return (1); return (1);