#361 closed defect (fixed)
psMetadataItemAllocV over-allocates memory for comment
| Reported by: | eugene | Owned by: | |
|---|---|---|---|
| Priority: | high | Milestone: | |
| Component: | types | Version: | 0.5.0 |
| Severity: | normal | Keywords: | |
| Cc: |
Description
the specific problem is in lines 187-194:
Allocate and set metadata item comment
metadataItem->comment = (char *)psAlloc(sizeof(char) * MAX_STRING_LENGTH);
if (comment == NULL) {
Per SDRS, null isn't allowed, must use "" instead
strncpy(metadataItem->comment, "", MAX_STRING_LENGTH);
} else {
strncpy(metadataItem->comment, comment, MAX_STRING_LENGTH);
}
this is allocating a potentially huge block of data, when we really only need a
minimal chunk which can in principal be measured by strlen().
the more fundamental problem is that the code is not making use of the low-level
functionalities already in psLib: psStringCopy. here is how I would have done
this:
set metadata item comment
if (comment == NULL) {
Per SDRS, null isn't allowed, must use "" instead
metadataItem->comment = psStringCopy ("");
} else {
metadataItem->comment = psStringCopy (comment);
}
(now, even if we have a blanket policy that strings are always allocated in
large chunks, for efficiency's sake, we have isolated that policy decision to a
single function).

implemented fix as suggested.