Using a string with libusb causes segmentation fault

0

I'm using netbeans with gcc to compile. When I run the attached code, I get the error: RUN FINISHED; Segmentation fault; core dumped; real time: 310ms; user: 40ms; system: 10ms

If 'char stringData' is commented out, there is no segmentation fault. Also, if libusb_get_device_descriptor is removed (and accompanying struct references), there is no segmentation fault.

If it's run in debug mode, it gives an error saying:

#error "Never use <bits/string_fortified.h> directly; include <string.h> instead."

I can't see how these could have any effect on each other.

#include <stdio.h>
#include <stdlib.h>
#include <libusb.h>
#define DATLENGTH 15

int main() {
    libusb_context *context;
    int i; 
    libusb_device **list;
    libusb_device *device;
    const struct libusb_version *libVersion;
    ssize_t numDev = 0;
    struct libusb_device_descriptor *dev_descriptor;
    struct libusb_config_descriptor *config;
    libusb_device_handle * dev_handle;
    char stringData[DATLENGTH];

    libusb_init(&context);


    libVersion = libusb_get_version();
    printf("Library Version: %i.%i.%i.%i\n", (int)libVersion->major, (int)libVersion->minor, (int)libVersion->micro, (int)libVersion->nano);

    numDev = libusb_get_device_list(context, &list);    
    if(numDev < 0){
        printf("Error %i\n", numDev);
        return -1;
    }
    else{
        printf("Number of devices: %i\n\n", numDev);
    }

    for(i=0;i<numDev;i++){    
        device = list[0];

        struct libusb_device_descriptor *dev_descriptor;

        libusb_get_device_descriptor(device, dev_descriptor);
        printf("Device #:  %i\n", i);
        printf("idVendor:  %04x\n", (dev_descriptor->idVendor & 0x0000FFFF));
        printf("idProduct: %04x\n", (dev_descriptor->idProduct & 0x0000FFFF));
        printf("Number of Configurations: %i\n", dev_descriptor->bNumConfigurations);
        libusb_open(device, &dev_handle);
        libusb_get_string_descriptor_ascii(dev_handle, dev_descriptor->iManufacturer, stringData, DATLENGTH);
        printf("Manufacturer ID: %s\n", stringData);        
        libusb_get_string_descriptor_ascii(dev_handle, dev_descriptor->iProduct, stringData, DATLENGTH);
        printf("Product ID: %s\n", stringData);       


        printf("\n");
        }

        libusb_free_device_list(list, 1); 

        libusb_exit(context);

    return (EXIT_SUCCESS);
}
c
string
libusb
asked on Stack Overflow Dec 17, 2018 by Gacekky1

1 Answer

1

You are producing Undefined Behaviour with:

struct libusb_device_descriptor *dev_descriptor;
libusb_get_device_descriptor(device, dev_descriptor);

dev_descriptor is expected to be a pointer to a struct libusb_device_descriptor. But it is a pointer to nowhere because you never initialized it. When libusb_get_device_descriptor attempts to store information in the struct, it will either segfault or overwrite random memory.

You should change it to:

struct libusb_device_descriptor dev_descriptor;
libusb_get_device_descriptor(device, &dev_descriptor);

which actually allocates a struct libusb_device_descriptor on the stack and then passes the address of the struct. Note that you will then have to change dev_descriptor->field to dev_descriptor.field throughout.

answered on Stack Overflow Dec 17, 2018 by rici

User contributions licensed under CC BY-SA 3.0