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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:43:03+00:00 2026-06-07T10:43:03+00:00

I am building an FTP client in C++ for personal use and for the

  • 0

I am building an FTP client in C++ for personal use and for the learning experience, but I have run into a problem when allocating memory for storing LIST responses. The library I am using for FTP requests is libcurl which will call the following function when it receives a response from the server:

size_t FTP_getList( char *ptr, size_t size, size_t nmemb, void *userdata) {
    //GLOBAL_FRAGMENT is global
    //libcurl will split the resulting list into smaller approx 2000 character
    //strings to pass into this function so I compensate by storing the leftover
    //fragment in a global variable.
    size_t fraglen = 0;
    if(GLOBAL_FRAGMENT!=NULL) {
        fraglen = strlen(GLOBAL_FRAGMENT);
    }
    size_t listlen = size*nmemb+fraglen+1;
    std::cout<<"Size="<<size<<" nmemb="<<nmemb;
    char *list = new char[listlen];
    if(GLOBAL_FRAGMENT!=NULL) {
        snprintf(list,listlen,"%s%s",GLOBAL_FRAGMENT,ptr);
    } else {
        strncpy(list,ptr,listlen);
    }
    list[listlen]=0;
    size_t packetSize = strlen(list);
    std::cout<<list;
    bool isComplete = false;
    //Check to see if the last line is complete (i.e. newline terminated)
    if(list[size]=='\n') {
        isComplete = true;
    }
    if(GLOBAL_FRAGMENT!=NULL) {
        delete[] GLOBAL_FRAGMENT;
    }
    GLOBAL_FRAGMENT = GLOBAL_FTP->listParse(list,isComplete);
    delete[] list;
    //We return the length of the new string to prove to libcurl we
    //our function properly executed
    return size*nmemb;
}

The function above calls the next function to split each line returned into individual
strings to be further processed:

char* FTP::listParse(char* list, bool isComplete) {
    //std::cout << list;
    //We split the list into seperate lines to deal with independently
    char* line = strtok(list,"\n"); 
    int count = 0;
    while(line!=NULL) {
        count++;
        line = strtok(NULL,"\n");
    }
    //std::cout << "List Count: " << count << "\n";
    int curPosition = 0;
    for(int i = 0; i < count-1 ; i++) {
        //std::cout << "Iteration: " << i << "\n";
        curPosition = curPosition + lineParse((char*)&(list[curPosition])) + 1; 
    }
    if(isComplete) {
        lineParse((char*)&(list[curPosition]));
        return NULL;
    } else {
        int fraglen = strlen((char*)&(list[curPosition]));
        char* frag = new char[fraglen+1];
        strcpy(frag,(char*)&(list[curPosition]));
        frag[fraglen] = 0;
        return frag;
    }
}

The function above then calls the function below to split the individual entries in a line into separate tokens:

int FTP::lineParse(char *line) {
    int result = strlen(line);
    char* value = strtok(line, " ");
    while(value!=NULL) {
        //std::cout << value << "\n";
        value = strtok(NULL, " ");
    }
    return result;
}

This program works for relatively small list responses but when I tried stress testing it by getting a listing for a remote directory with ~10,000 files in it, my program threw a SIGSEGV… I used backtrace in gdb and found that the segfault happens on lines delete[] GLOBAL_FRAGMENT;' anddelete[] list;inFTP_getList. Am I not properly deleting these arrays? I am callingdelete[]` exactly once for each time I allocate them so I don’t see why it wouldn’t be allocating memory correctly…

On a side note: Is it necessary to check to see if an array is NULL before you try to delete it?

Also, I know this would be easier to do with STD::Strings but I am trying to learn c style strings as practice, and the fact that it is crashing is a perfect example of why I need practice, I will also be changing the code to store these in a dynamically allocated buffer that only is reallocated when the new ptr size is larger than the previous length, but I want to figure out why the current code isn’t working first. 🙂 Any help would be appreciated.

  • 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-07T10:43:05+00:00Added an answer on June 7, 2026 at 10:43 am

    In this code

    size_t listlen = size*nmemb+fraglen+1;
    std::cout<<"Size="<<size<<" nmemb="<<nmemb;
    char *list = new char[listlen];
    if(GLOBAL_FRAGMENT!=NULL) {
        snprintf(list,listlen,"%s%s",GLOBAL_FRAGMENT,ptr);
    } else {
        strncpy(list,ptr,listlen);
    }
    list[listlen]=0;
    

    You are overruning your list buffer. You have allocated listlen bytes, but you write a 0 value one past the last allocated byte. This invokes undefined behavior. More practically speaking, it can cause heap corruption, which can cause the kind of errors you observed.

    I didn’t see any issues with the way you are calling delete[].

    It is perfectly safe to delete a NULL pointer.

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

Sidebar

Related Questions

I'm building an FTP client from scratch and I've noticed that the response codes
Building a commercial product may use various open source libraries that have use of
I'm really wondering which is more stable in building FTP client/server architecture.
I am building a simple client-server program , I have in main : FTPClient
The old question is below. The problem was ftp transfer mode wasn't binary. But
Building on this question , now I have another problem. Given this, shipments =
I am building an FTP utility class in C#. In the case that a
Building an inventory system. I have lots of products and each product has three
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
I'm building a (very) simple FTP app in Cocoa, and I need to store

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.