-
Notifications
You must be signed in to change notification settings - Fork 608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Use spans to solve a number of memory safety issues #4148
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -639,85 +639,117 @@ add_exif_item_to_spec(ImageSpec& spec, const char* name, | |
int offset_adjustment = 0) | ||
{ | ||
OIIO_ASSERT(dirp); | ||
const uint8_t* dataptr = (const uint8_t*)pvt::dataptr(*dirp, buf, | ||
offset_adjustment); | ||
if (!dataptr) | ||
return; | ||
TypeDesc type = tiff_datatype_to_typedesc(*dirp); | ||
size_t count = dirp->tdir_count; | ||
if (dirp->tdir_type == TIFF_SHORT) { | ||
std::vector<uint16_t> d((const uint16_t*)dataptr, | ||
(const uint16_t*)dataptr + dirp->tdir_count); | ||
if (swab) | ||
swap_endian(d.data(), d.size()); | ||
spec.attribute(name, type, d.data()); | ||
cspan<uint8_t> dspan | ||
= pvt::dataspan<uint16_t>(*dirp, buf, offset_adjustment, count); | ||
if (dspan.empty()) | ||
return; | ||
if (swab) { | ||
// In the byte swap case, copy it into a vector because the | ||
// upstream source isn't mutable. | ||
std::vector<uint16_t> dswab((const uint16_t*)dspan.begin(), | ||
(const uint16_t*)dspan.end()); | ||
Comment on lines
+652
to
+653
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is making a copy of dspan when previously no copy was used. Is that acceptable time and memory overhead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's the opposite. Previously, it was ALWAYS making a copy into a vector (a few lines above). Now it only does so if it needs to mutate the data by byteswapping it. Now when there is no swap needed, there is no copy. |
||
swap_endian(dswab.data(), dswab.size()); | ||
spec.attribute(name, type, dswab.data()); | ||
} else { | ||
spec.attribute(name, type, dspan.data()); | ||
} | ||
return; | ||
} | ||
if (dirp->tdir_type == TIFF_LONG) { | ||
std::vector<uint32_t> d((const uint32_t*)dataptr, | ||
(const uint32_t*)dataptr + dirp->tdir_count); | ||
if (swab) | ||
swap_endian(d.data(), d.size()); | ||
spec.attribute(name, type, d.data()); | ||
cspan<uint8_t> dspan | ||
= pvt::dataspan<uint32_t>(*dirp, buf, offset_adjustment, count); | ||
if (dspan.empty()) | ||
return; | ||
if (swab) { | ||
// In the byte swap case, copy it into a vector because the | ||
// upstream source isn't mutable. | ||
std::vector<uint32_t> dswab((const uint32_t*)dspan.begin(), | ||
(const uint32_t*)dspan.end()); | ||
swap_endian(dswab.data(), dswab.size()); | ||
spec.attribute(name, type, dswab.data()); | ||
} else { | ||
spec.attribute(name, type, dspan.data()); | ||
} | ||
return; | ||
} | ||
if (dirp->tdir_type == TIFF_RATIONAL) { | ||
int n = dirp->tdir_count; // How many | ||
float* f = OIIO_ALLOCA(float, n); | ||
for (int i = 0; i < n; ++i) { | ||
cspan<uint8_t> dspan | ||
= pvt::dataspan<uint32_t>(*dirp, buf, offset_adjustment, 2 * count); | ||
if (dspan.empty()) | ||
return; | ||
float* f = OIIO_ALLOCA(float, count); | ||
for (size_t i = 0; i < count; ++i) { | ||
// Because the values in the blob aren't 32-bit-aligned, memcpy | ||
// them into ints to do the swapping. | ||
unsigned int num, den; | ||
memcpy(&num, dataptr + (2 * i) * sizeof(unsigned int), | ||
sizeof(unsigned int)); | ||
memcpy(&den, dataptr + (2 * i + 1) * sizeof(unsigned int), | ||
memcpy(&num, dspan.data() + (2 * i) * sizeof(unsigned int), | ||
sizeof(unsigned int)); //NOSONAR | ||
memcpy(&den, dspan.data() + (2 * i + 1) * sizeof(unsigned int), | ||
sizeof(unsigned int)); //NOSONAR | ||
if (swab) { | ||
swap_endian(&num); | ||
swap_endian(&den); | ||
num = byteswap(num); | ||
den = byteswap(den); | ||
} | ||
f[i] = (float)((double)num / (double)den); | ||
} | ||
if (dirp->tdir_count == 1) | ||
spec.attribute(name, *f); | ||
spec.attribute(name, f[0]); | ||
else | ||
spec.attribute(name, TypeDesc(TypeDesc::FLOAT, n), f); | ||
spec.attribute(name, TypeDesc(TypeDesc::FLOAT, int(count)), f); | ||
return; | ||
} | ||
if (dirp->tdir_type == TIFF_SRATIONAL) { | ||
int n = dirp->tdir_count; // How many | ||
float* f = OIIO_ALLOCA(float, n); | ||
for (int i = 0; i < n; ++i) { | ||
cspan<uint8_t> dspan | ||
= pvt::dataspan<int32_t>(*dirp, buf, offset_adjustment, 2 * count); | ||
if (dspan.empty()) | ||
return; | ||
float* f = OIIO_ALLOCA(float, count); | ||
for (size_t i = 0; i < count; ++i) { | ||
// Because the values in the blob aren't 32-bit-aligned, memcpy | ||
// them into ints to do the swapping. | ||
int num, den; | ||
memcpy(&num, dataptr + (2 * i) * sizeof(int), sizeof(int)); | ||
memcpy(&den, dataptr + (2 * i + 1) * sizeof(int), | ||
memcpy(&num, dspan.data() + (2 * i) * sizeof(int), | ||
sizeof(int)); //NOSONAR | ||
memcpy(&den, dspan.data() + (2 * i + 1) * sizeof(int), | ||
sizeof(int)); //NOSONAR | ||
if (swab) { | ||
swap_endian(&num); | ||
swap_endian(&den); | ||
num = byteswap(num); | ||
den = byteswap(den); | ||
} | ||
f[i] = (float)((double)num / (double)den); | ||
} | ||
if (dirp->tdir_count == 1) | ||
spec.attribute(name, *f); | ||
spec.attribute(name, f[0]); | ||
else | ||
spec.attribute(name, TypeDesc(TypeDesc::FLOAT, n), f); | ||
spec.attribute(name, TypeDesc(TypeDesc::FLOAT, int(count)), f); | ||
return; | ||
} | ||
if (dirp->tdir_type == TIFF_ASCII) { | ||
int len = tiff_data_size(*dirp); | ||
const char* ptr = (const char*)dataptr; | ||
while (len && ptr[len - 1] == 0) // Don't grab the terminating null | ||
--len; | ||
std::string str(ptr, len); | ||
size_t len = tiff_data_size(*dirp); | ||
cspan<uint8_t> dspan = pvt::dataspan<char>(*dirp, buf, | ||
offset_adjustment, len); | ||
if (dspan.empty()) | ||
return; | ||
// Don't grab the terminating null | ||
while (dspan.size() && dspan.back() == 0) | ||
dspan = dspan.subspan(0, dspan.size() - 1); | ||
std::string str(dspan.begin(), dspan.end()); | ||
if (strlen(str.c_str()) < str.length()) // Stray \0 in the middle | ||
str = std::string(str.c_str()); | ||
spec.attribute(name, str); | ||
return; | ||
} | ||
if (dirp->tdir_type == TIFF_BYTE && dirp->tdir_count == 1) { | ||
if (dirp->tdir_type == TIFF_BYTE && count == 1) { | ||
// Not sure how to handle "bytes" generally, but certainly for just | ||
// one, add it as an int. | ||
unsigned char d; | ||
d = *dataptr; // byte stored in offset itself | ||
spec.attribute(name, (int)d); | ||
cspan<uint8_t> dspan = pvt::dataspan<uint8_t>(*dirp, buf, | ||
offset_adjustment, count); | ||
if (dspan.empty()) | ||
return; | ||
spec.attribute(name, (int)dspan[0]); | ||
return; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is dspan 8-bit when it's given a 16-bit span and dswab uses it as if it was 16-bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's in the middle of some byte buffer, they aren't actually necessarily aligned on 16-bit boundaries. We're just copying things around, a pile of bytes is ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add some comments to clarify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated with better comments.