Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:58:03+00:00 2026-06-17T04:58:03+00:00

I’m fairly new to C, and I’m trying to write a utility to open

  • 0

I’m fairly new to C, and I’m trying to write a utility to open all event handlers which handle EV_KEY events, and give me a list of their file descriptors (while they’re still open). The function which does all of that appears to be functioning properly, when when I try to loop over the file descriptors, I’m getting a smashed stack error. I tried compiling each C file with gcc’s -Wall flag to find what might be causing the problem, but it hasn’t helped me. Here’s my code:

int *get_key_handlers() {

    int *fds;
    int fd;
    int num_handlers = 0;
    struct dirent *dp;
    DIR *dir;
    char full_path[100];
    char *base_path = "/dev/input/";

    printf("Creating file descriptor array\n");
    fds = malloc(sizeof(int*)*32);   // not currently going to bother
                                     //   with more than 32 handlers

    printf("Opening event handler directory\n");
    if (!(dir = opendir(base_path))) {
        return 1;
    }

    printf("Beginning reading through directory\n");
    while ((dp = readdir(dir))) {

        if (dp->d_name && !strncmp(dp->d_name, "event", 5)) {

            // cat the base path and device name, store in full_path
            snprintf(full_path, sizeof(full_path), "%s%s", base_path, dp->d_name);
            printf("Found handler at %s\n", full_path);
            fd = open(full_path, O_RDONLY);

            printf("Detecting handler features...\n");
            unsigned char results[EV_MAX/8+1];
            memset(results, 0, sizeof(results));
            ioctl(fd, EVIOCGBIT(EV_KEY, EV_MAX), results); 
            printf("Features determined\n");
            printf("EV_KEY: %d\n", EV_KEY);
            if (test_bit(*results, EV_KEY)) {
                printf("EV_KEY feature detected on %s\n", full_path);
                fds[num_handlers] = fd;
                num_handlers++;
            } else {
                printf("EV_KEY feature NOT detected on %s\n", full_path);
                close(fd);
            }
            printf("Done with handler at %s\n", full_path);
        }
    }
    printf("Finished creating handler list\n");

    fds = realloc(fds, sizeof(int*) * num_handlers);
    printf("Handler list re-sized\n");
    return fds;
}

Here is the code that calls the get_key_handlers() function:

int main() {
    int *fds = get_key_handlers();
    int i = 0;
    for (i = 0; i < (sizeof(fds)/sizeof(int*)); i++) {
        printf("Next file descriptor: %d\n", *(fds + (i * sizeof(int*))));
    }

    free(fds);
    return 0;
}

Last of all, here are the commands I’m running to compile and link, along with runtime output of the program:

tim@tim-ubuntu:~/Documents/Programming/C/clogger$ gcc -c -Wall thinput.c
thinput.c: In function ‘get_key_handlers’:
thinput.c:21:9: warning: return makes pointer from integer without a cast [enabled by default]
tim@tim-ubuntu:~/Documents/Programming/C/clogger$ gcc -c -Wall thinputtest.c
tim@tim-ubuntu:~/Documents/Programming/C/clogger$ gcc thinput.o thinputtest.o -o thinputtest
tim@tim-ubuntu:~/Documents/Programming/C/clogger$ sudo ./thinputtest
Creating file descriptor array
Opening event handler directory
Beginning reading through directory
Found handler at /dev/input/event15
Detecting handler features...
Features determined
EV_KEY: 1
EV_KEY feature NOT detected on /dev/input/event15
Done with handler at /dev/input/event15
Found handler at /dev/input/event14
Detecting handler features...
Features determined
EV_KEY: 1
EV_KEY feature detected on /dev/input/event3
Done with handler at /dev/input/event3
Found handler at /dev/input/event2
Detecting handler features...
Features determined
EV_KEY: 1
EV_KEY feature NOT detected on /dev/input/event1
Done with handler at /dev/input/event1
Found handler at /dev/input/event0
Detecting handler features...
Features determined
EV_KEY: 1
EV_KEY feature NOT detected on /dev/input/event0
Done with handler at /dev/input/event0
Finished creating handler list
Handler list re-sized
*** stack smashing detected ***: ./thinputtest terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7f0ef8658807]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x0)[0x7f0ef86587d0]
./thinputtest[0x400ac7]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:02 7215828                            /home/tim/Documents/Programming/C/clogger/thinputtest
00601000-00602000 r--p 00001000 08:02 7215828                            /home/tim/Documents/Programming/C/clogger/thinputtest
00602000-00603000 rw-p 00002000 08:02 7215828                            /home/tim/Documents/Programming/C/clogger/thinputtest
01b88000-01ba9000 rw-p 00000000 00:00 0                                  [heap]
7f0ef8338000-7f0ef834d000 r-xp 00000000 08:02 5247337                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0ef834d000-7f0ef854c000 ---p 00015000 08:02 5247337                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0ef854c000-7f0ef854d000 r--p 00014000 08:02 5247337                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0ef854d000-7f0ef854e000 rw-p 00015000 08:02 5247337                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0ef854e000-7f0ef8703000 r-xp 00000000 08:02 5247958                    /lib/x86_64-linux-gnu/libc-2.15.so
7f0ef8703000-7f0ef8902000 ---p 001b5000 08:02 5247958                    /lib/x86_64-linux-gnu/libc-2.15.so
7f0ef8902000-7f0ef8906000 r--p 001b4000 08:02 5247958                    /lib/x86_64-linux-gnu/libc-2.15.so
7f0ef8906000-7f0ef8908000 rw-p 001b8000 08:02 5247958                    /lib/x86_64-linux-gnu/libc-2.15.so
7f0ef8908000-7f0ef890d000 rw-p 00000000 00:00 0 
7f0ef890d000-7f0ef892f000 r-xp 00000000 08:02 5255999                    /lib/x86_64-linux-gnu/ld-2.15.so
7f0ef8b08000-7f0ef8b0b000 rw-p 00000000 00:00 0 
7f0ef8b2b000-7f0ef8b2f000 rw-p 00000000 00:00 0 
7f0ef8b2f000-7f0ef8b30000 r--p 00022000 08:02 5255999                    /lib/x86_64-linux-gnu/ld-2.15.so
7f0ef8b30000-7f0ef8b32000 rw-p 00023000 08:02 5255999                    /lib/x86_64-linux-gnu/ld-2.15.so
7fffbd9b4000-7fffbd9d5000 rw-p 00000000 00:00 0                          [stack]
7fffbd9ff000-7fffbda00000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

Thank you very much in advance for any time you spend helping me figure this out; I appreciate any advice or critiques sent my way!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:58:04+00:00Added an answer on June 17, 2026 at 4:58 am

    The ioctl has the wrong size:

    ioctl(fd, EVIOCGBIT(EV_KEY, EV_MAX), results);
    

    change that line to:

    ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(results)), results);
    

    You are indirectly writing outside results’ bounds by telling EVIOCGBIT that it has EV_MAX bytes to fill while it only has EV_MAX÷8, thus your stack is trashed. Due to the way stack smashing detection works it can only be detected once you return.

    To get you convinced, have a look at the comment in drivers/input/evdev.c, it says:

    /*
     * Work around bugs in userspace programs that like to do
     * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
     * should be in bytes, not in bits.
     */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.