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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:40:24+00:00 2026-06-16T08:40:24+00:00

I am trying to write a function that searches for all occurrences of a

  • 0

I am trying to write a function that searches for all occurrences of a pattern and returns an array of offsets in the file that match the pattern. I want to use realloc to dynamically grow my returned array, but I am getting a sYSMALLOC Assertion error. Sometimes if I use a different search pattern I might get an invalid next size error. I do not have valgrind on my machine yet (would require rebuilding glibc with debug flag). This seems like such a simple issue, I only touch this pointer twice – once to declare it and set it to NULL and again to realloc to grow it. I know that sizeof(char) is 1, and is useless to have in my realloc statement.

Here is the code for the function having problems.

unsigned long long* searchBytes(FILE* fp, char* byteString, char* searchType, unsigned   long long fileSize)
{
if (fp == NULL)
    return NULL;

unsigned long long* foundBytes = NULL;
long numBytes = 0;

// make some memory for the array of found bytes
if (strcmp(searchType, "ascii") == 0)
{
    numBytes = strlen(byteString);
    //foundBytes = realloc(NULL, numBytes * sizeof(char));
}
else
{
    // TODO strip the spaces from the string and handle hex searches
    printf("hex-search not implemented yet.\n");
    return NULL;
}

// loop over all the bytes in the file looking for this ascii pattern
unsigned long long currentOffset = 0;
unsigned long long origOffset = 0;
unsigned long long m = 0;
foundWords = 0;
char* possibleWord = malloc(numBytes * sizeof(char));

do
{
    fseek(fp, currentOffset, SEEK_SET);
    unsigned long long i;
    int n = 0;
    int failed = 0;
    origOffset = currentOffset;

    for(i=currentOffset; i<currentOffset+numBytes; i++)
    {
        possibleWord[n] = fgetc(fp);
        n++;
    }
    //printf("possibleWord: %s\n", possibleWord);

    // is this our word? use strstr just in case
    char* found = strstr((const char*) byteString, (const char*) possibleWord);
    if (found)
    {
        foundWords++;
        // make a bigger spot for it
        printf("allocating %ld bytes to add word %d to list...\n", (numBytes*foundWords) * sizeof(char), foundWords);
        unsigned long long* p = realloc(foundBytes, (numBytes*foundWords) * sizeof(char));
        if (p)
        {
            foundBytes = p;

            for (i = origOffset; i<origOffset+numBytes; i++)
            {
                foundBytes[m] = i;
                //printf("added offset %llu to foundBytes[%llu]\n", i, m);
                m++;
            }

        }
        else
        {
            return NULL;
        }

    }
    else
    {
        failed = 1;
    }

    if (failed == 0)
    {
        currentOffset += numBytes;
        //printf("Yay! moving forward %ld bytes.\n", numBytes);
    }
    else
    {
        currentOffset++;
    }   
}
while (currentOffset < fileSize);

if (foundWords > 0)
{
    //printf("returning foundBytes!\n");

    //unsigned long long z;
    //for (z=0; z<foundWords*numBytes; z++)
    //  printf("%llu\n", foundBytes[z]);
    //printf("...\n");
    return foundBytes;
}
//printf("returning NULL\n");
return NULL;
}

when run on /etc/passwd using “root” as search pattern:

allocating 4 bytes to add word 1 to list...
allocating 8 bytes to add word 2 to list...
*** glibc detected *** ./chex3: realloc(): invalid next size: 0x0000000001a59270 ***

or on /etc/passwd using “daemon” as search pattern:

allocating 6 bytes to add word 1 to list...
allocating 12 bytes to add word 2 to list...
chex3: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

Can someone look at this and see if it looks OK? Thanks! I am a noob trying to learn 🙂

  • 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-16T08:40:25+00:00Added an answer on June 16, 2026 at 8:40 am

    The solution was to decalure p up top with foundBytes and then change the realloc line to:

    p = realloc(foundBytes, (numBytes*foundWords) * sizeof(*p));
    

    I must admit I don’t really understand why this is necessary, it seems like I allocating a lot more than enough this way. (adding 24 bytes each time instead of 3 for a 3 character search pattern)

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

Sidebar

Related Questions

I'm trying to write a function that returns a list of all files on
I am trying to write a function that includes a file named header.php in
Trying to write function that numerize items of list and returns list of tuples
I am trying to write a function that takes File object, offset and byte
I'm trying to write a function that accepts an int n and returns a
Im trying to write a function that will convert all charactewrs after the first
I am trying to write a jQuery function which searches for all divs on
I have been trying to write a recursive function that searches a stack, but
I am trying write a PHP function that returns a random string of a
So i am trying to make a function that searches trough an xml file,

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.