• Uncategorized

About c : ClamAV-Cant-allocate-memory

Question Detail

I am writing a function in C and it’ll work on Debian 8. What the function does is just downloading a file and scanning it by using cURL and ClamAV. So, there are two functions I wrote url2file() and clamav_scan(). And I want to insert url2file() into clamav_scan(). But, it’ll show an error message ‘Can’t allocate memory’… why? If I delete a line url2file() in clamav_scan(), it works very well.

This below is my code I’ve written.

Thank you.

// this function scans a file by using ClamAV
int clamav_scan(const char* file_name)
{
    int ret = 0;

    struct cl_engine* engine = NULL;   // clamAV engine
    unsigned int signo = 0;            // clamAV signature number
    const char* path = NULL;           // clamAV DB path

    const char* virname = NULL;        // virus name
    unsigned long int scanned = 0;     // size of scanned data

    char target[MAX_PATH] = "./";      // path of the scan file


    strcat(target, file_name);       

    if ((ret = cl_init(CL_INIT_DEFAULT)) != CL_SUCCESS) {
        printf("cl_init() failed: %s\n", cl_strerror(ret));
        exit(1);
    }

    if ((engine = cl_engine_new()) == NULL) {
        printf("cl_engine_new() failed\n");
        exit(1);
    }

    if ((path = cl_retdbdir()) == NULL) {
        printf("cl_retdbdir() failed\n");
        cl_engine_free(engine);
        exit(1);
    }

    if ((ret = cl_load(path, engine, &signo, CL_DB_STDOPT)) != CL_SUCCESS) {
        printf("cl_load() failed: %s\n", cl_strerror(ret));
        cl_engine_free(engine);
        exit(1);
    }

    if ((ret = cl_engine_compile(engine)) != CL_SUCCESS) {
        printf("cl_engine_compile() failed: %s\n", cl_strerror(ret));
        cl_engine_free(engine);
        exit(1);
    }

    // this is the inserted function I said
    // this function downloads a file by using cURL
    url2file(url, file_name);

    printf("Scanning %s ...\n", target);
    if ((ret = cl_scanfile((const char*)target, &virname, &scanned, engine, CL_SCAN_STDOPT)) == CL_VIRUS) {
        printf("Virus detected: %s\n", virname);
    }
    else {
        printf("No virus detected\n");
        if (ret != CL_CLEAN)
            printf("Error: %s\n", cl_strerror(ret));    // shows message here
    }
    printf("scanned: %lu\n", scanned);

    cl_engine_free(engine);

    return EXIT_SUCCESS;
}

I referred to

https://curl.haxx.se/libcurl/c/url2file.html

https://raw.githubusercontent.com/vrtadmin/clamav-faq/master/manual/clamdoc.pdf

.

.

.

+++ added url2file() function

static size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream)
{
    size_t written = fwrite(ptr, size, nmemb, (FILE*)stream);
    return written;
}

int url2file(const char* url, const char* pagefilename)
{
    CURL *curl_handle;
    FILE *pagefile;

    curl_global_init(CURL_GLOBAL_ALL);

    /* init the curl session */ 
    curl_handle = curl_easy_init();

    /* set URL to get here */ 
    curl_easy_setopt(curl_handle, CURLOPT_URL, url);

    /* Switch on full protocol/debug output while testing */ 
    curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

    /* disable progress meter, set to 0L to enable and disable debug output */ 
    curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

    /* send all data to this function  */ 
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

    /* open the file */ 
    pagefile = fopen(pagefilename, "wb");
    if (pagefile) {
        /* write the page body to this file handle */ 
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

        /* get it! */ 
        curl_easy_perform(curl_handle);

        /* close the header file */ 
        fclose(pagefile);
    }

    /* cleanup curl stuff */ 
    curl_easy_cleanup(curl_handle);

    curl_global_cleanup();

    return 0;
}

Question Answer

No answer for now.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.