在libusb中使用字符串会导致分段错误

时间:2018-12-17 01:47:56

标签: c string libusb

我正在使用带有gcc的netbeans进行编译。当我运行附带的代码时,出现错误: RUN FINISHED; Segmentation fault; core dumped; real time: 310ms; user: 40ms; system: 10ms

如果注释了'char stringData',则没有分段错误。另外,如果删除libusb_get_device_descriptor(以及随附的结构引用),则不会出现分段错误。

如果它在调试模式下运行,则会显示一条错误消息:

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

我看不到它们如何相互影响。

#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);
}

1 个答案:

答案 0 :(得分:1)

您正在使用以下方法产生未定义的行为

const renderer = createBundleRenderer(bundle.server, {
    runInNewContext: false,
    inject: false,
    template: `<!DOCTYPE html>
        <html>
            <head>
                {{{ head }}}
                {{{ renderResourceHints() }}}
                {{{ renderStyles() }}}
            </head>
            <body>
                <!--vue-ssr-outlet-->
                <script>${ bundle.client }</script>
            </body>
       </html>`
})

struct libusb_device_descriptor *dev_descriptor; libusb_get_device_descriptor(device, dev_descriptor); 应该是指向dev_descriptor的指针。但这是指向无处的指针,因为您从未初始化过它。当struct libusb_device_descriptor尝试在结构中存储信息时,它将出现段错误或覆盖随机存储器。

您应该将其更改为:

libusb_get_device_descriptor

实际在堆栈上分配一个struct libusb_device_descriptor dev_descriptor; libusb_get_device_descriptor(device, &dev_descriptor); ,然后传递该结构的地址。请注意,然后您必须将struct libusb_device_descriptor更改为dev_descriptor->field