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

  • Home
  • SEARCH
  • 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 7639695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:29:06+00:00 2026-05-31T08:29:06+00:00

I am parsing a macro definition from a Makefile into two strings, the name

  • 0

I am parsing a macro definition from a Makefile into two strings, the name of the macro and the body. For example here is a macro definition line from my Makefile:

macro-1 = body-1

My code produces a bus error/segmentation fault.

static void parse_macro_def(const char* line)
{
   char* m_name;
   int name_pos = 0;

   int i = 0;
   while(line[i++] != '=')                    //iterate until an equal sign is found
   {
      if(!isspace(line[i]))          //copy characters to m_name  unless the character is whitespace
      {
         m_name[name_pos++] = line[i];
      }
   }
}
m_name[name_pos] = '\0';

m_name should be set to macro-1
Thanks for all the help!

  • 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-31T08:29:07+00:00Added an answer on May 31, 2026 at 8:29 am

    You don’t initialize m_name so it points anywhere, so you are writing at random, and crashing.

    You need to allocate and return the space, or pass the space in (along with the length of the space), ensuring you don’t overwrite in every case.


    You should note that white space characters in a macro name are a bug in the macro definition. There can be leading white space; there can be trailing white space; but there can’t be white space in the middle of a name. Now, if you are assuming that you have been given a valid, working Makefile to parse, you may be able to get away with ignoring this subtlety. If you’re writing a replacement for make, you can’t.

    Unless you’ve previously validated that there is an equals sign in the string, you should also check that you don’t run off the end of the string (you don’t scan past a NUL '\0'). In fact, in robust code, you’d probably ensure that out of paranoia.

    while (line[i] != '\0' && line[i] != '=')
    {
        ...
    }
    

    And, while writing that, I realized that you increment i in the while condition, and then check whether the next character is a space in the body of the loop. That’s a little aconventional, shall we say. If you come across a macro:

    MACRO=value
    

    you will copy the = into m_name when the loop condition checks the O. And, AFAICS, you won’t copy the M.


    Note that your line:

    m_name[name_pos] = '\0';
    

    is outside any function and therefore a syntax error.

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

Sidebar

Related Questions

Here's some example code to give an idea of what I want. int regular_function(void)
I'm parsing text from a file and storing it in a string. The problem
I am parsing an RSS feed from the following URL: http://rss.sciam.com/ScientificAmerican-Global?format=xml // $xml_text is
I am using C# interop to call a macro from C#. This works fine.
Parsing JSON seems like it is a pretty common topic of discussion on here.
I'm working on a macro system for Python ( as discussed here ) and
I have an XML-like file: <p>something</p> <ac:image> <ri:attachment ri:filename=IMAGE.PNG /> </ac:image> <ac:macro ac:name=screenshot> <ac:default-parameter>IMAGE.ss</ac:default-parameter>
Parsing some webpages with nokogiri, i've got some issues while cleaning some Strings and
Parsing a text file in vb.net and need to locate the latitude and longitude
For parsing player commands, I've most often used the split method to split a

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.