[Mesa-dev] [PATCH 08/17] loader: Fix compiler warnings about truncating the PCI ID path.

Ian Romanick idr at freedesktop.org
Thu Feb 15 01:48:43 UTC 2018


On 02/13/2018 02:31 AM, Eric Anholt wrote:
> Ian Romanick <idr at freedesktop.org> writes:
> 
>> On 02/10/2018 08:33 AM, Eric Anholt wrote:
>>> My build was producing:
>>>
>>> ../src/loader/loader.c:121:67: warning: ā€˜%1uā€™ directive output may be truncated writing between 1 and 3 bytes into a region of size 2 [-Wformat-truncation=]
>>>
>>> and we can avoid this careful calculation by just using asprintf (as we do
>>> elsewhere in the file).
>>>
>>> Cc: Eric Engestrom <eric.engestrom at imgtec.com>
>>> ---
>>>  src/loader/loader.c | 15 +++++++--------
>>>  1 file changed, 7 insertions(+), 8 deletions(-)
>>>
>>> diff --git a/src/loader/loader.c b/src/loader/loader.c
>>> index 913b3dcac032..92b4c5204b19 100644
>>> --- a/src/loader/loader.c
>>> +++ b/src/loader/loader.c
>>> @@ -110,17 +110,16 @@ static char *loader_get_dri_config_device_id(void)
>>>  
>>>  static char *drm_construct_id_path_tag(drmDevicePtr device)
>>>  {
>>> -#define PCI_ID_PATH_TAG_LENGTH sizeof("pci-xxxx_xx_xx_x")
>>>     char *tag = NULL;
>>>  
>>>     if (device->bustype == DRM_BUS_PCI) {
>>> -        tag = calloc(PCI_ID_PATH_TAG_LENGTH, sizeof(char));
>>> -        if (tag == NULL)
>>> -            return NULL;
>>> -
>>> -        snprintf(tag, PCI_ID_PATH_TAG_LENGTH, "pci-%04x_%02x_%02x_%1u",
>>> -                 device->businfo.pci->domain, device->businfo.pci->bus,
>>> -                 device->businfo.pci->dev, device->businfo.pci->func);
>>> +      if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
>>> +                   device->businfo.pci->domain,
>>> +                   device->businfo.pci->bus,
>>> +                   device->businfo.pci->dev,
>>> +                   device->businfo.pci->func) < 0) {
>>> +         return NULL;
>>
>> Won't tag be NULL on failure?  Then you could just
>>
>>       asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
>>                device->businfo.pci->domain,
>>                device->businfo.pci->bus,
>>                device->businfo.pci->dev,
>>                device->businfo.pci->func);
>>       return tag;
> 
> "If memory allocation wasn't possible, or some other error occurs, these
> functions will return -1, and the contents of strp are undefined."

Boo.

Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>



-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: OpenPGP digital signature
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180214/0b108843/attachment.sig>


More information about the mesa-dev mailing list