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 6225915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:55:56+00:00 2026-05-24T08:55:56+00:00

Posting snippets of my code here. I am trying to get a footing around

  • 0

Posting snippets of my code here. I am trying to get a footing around debugging.

struct dirent *s_dirent;
char path[300];
....
bzero(path,300);
...
fd_dir = opendir(path);
while((s_dirent = readdir(fd_dir))!=NULL)
{
     if(s_dirent->d_name[0] == '.')
          continue;
     else
          break;
 }
if(s_dirent == NULL)
{
   if(closedir(fd_dir)!=0)
       perror("Error on closedir");
 }
else
{

  if(closedir(fd_dir)!=0)/*Line number 249*/
      perror("Error on closedir");

  /*some comments*/
  strcat(path,"/");
  strcat(path,s_dirent->d_name);/*Line number 254*/
 }

Valgrind output:

==3287== Invalid read of size 1
==3287==    at 0x40069E0: strcat (mc_replace_strmem.c:176)
==3287==    by 0x804D6B4: online_bck (backup_manager.c:254)
==3287==    by 0x8049F96: on_bck_beg (TxFS_manager.c:181)
==3287==    by 0x8049818: handler (Reader.c:236)
==3287==    by 0xBF5F18: start_thread (in /lib/libpthread-2.12.90.so)
==3287==    by 0xB37A2D: clone (in /lib/libc-2.12.90.so)
==3287==  Address 0x402a39b is 35 bytes inside a block of size 32,792 free'd
==3287==    at 0x40057F6: free (vg_replace_malloc.c:325)
==3287==    by 0xAF6C67: closedir (in /lib/libc-2.12.90.so)
==3287==    by 0x804D65A: online_bck (backup_manager.c:249)
==3287==    by 0x8049F96: on_bck_beg (TxFS_manager.c:181)
==3287==    by 0x8049818: handler (Reader.c:236)
==3287==    by 0xBF5F18: start_thread (in /lib/libpthread-2.12.90.so)
==3287==    by 0xB37A2D: clone (in /lib/libc-2.12.90.so)

Any help will be highly appreciated.
Thanks

  • 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-05-24T08:55:57+00:00Added an answer on May 24, 2026 at 8:55 am

    You should not access data, returned by readdir() after calling a closedir(). This is because closedir() may free any resources (e.g. memory), allocated in opendir/readdir.

    If you want to save dirent* struct, you can switch to readdir_r variant of readdir (with different set of parameters).

    UPDATE: Decoding of Valgrind output:

            V - note single space here; it is beginning of error message. 
    ==3287== Invalid read of size 1
    ==3287==    at 0x40069E0: strcat (mc_replace_strmem.c:176)
    
     backtrace skipped
    

    Valgrind says that error is Reading of invalid data, sized 1 byte, which is not a Memory Leak. It is incorrect memory access. And the actor of this read is the strcat() (called by skipped backtrace). Why the data is invalid? There is submessage

            VV - note two spaces here, it is continuation of error message
    ==3287==  Address 0x402a39b is 35 bytes inside a block of size 32,792 free'd
    ==3287==    at 0x40057F6: free (vg_replace_malloc.c:325)
    ==3287==    by 0xAF6C67: closedir (in /lib/libc-2.12.90.so)
    

    The byte is invalid (not allowed to be read from) because it is part of memory segment which was free-d (you can’t read from the memory you just free-d). Whom did this? Look at backtrace: closedir was the caller of free.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to test this code snippet but I get the Missing a using
I don't like pasting rather long code snippets in here, but I have a
First time posting here so take it easy with me, lol. I'm currently trying
In P.S. of this posting you can find a C# code snippet, which uses
My JQuery code is posting, because the success function is firing. The problem is
I am relatively new to Java and while trying out some code came across
Before posting my question to the ActiveState forum, I'd like to try luck here
I am working on ROT13 for c++ practice. however this bit of code here
I'm writing some documentation that will occasionally include C# or C++ code snippets. In
I'm a newbie programmer posting here for the first time. Any suggestions or advice

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.