bugGNU Gengen - Bugs: bug #38223, Error in generated code: passing...

 
 

bug #38223: Error in generated code: passing non-strings to generate_string()

Submitter:  Tim Marston <edam>
Submitted:  Fri 01 Feb 2013 03:18:37 PM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  None
Privacy:  Public Assigned to:  bettini
Open/Closed:  Open
* Mandatory Fields

Add a New Comment Rich Markup
   

Sat 16 Feb 2013 01:43:57 PM UTC, comment #4: 

Hi, I've adding a two new tests, test_types_tabs and test_types_tabs_c, which use the source code from test_types.cc and test_types_c.c, respectively (so there is no new source code for these tests).

It's a good job that I did, because I also discovered that my previous patch only half-worked when generating C source code.  Integer and boolean variables also don't want to be passed though outputcvar_gen_class, which is for strings.  So, for example, the following was generated for integers:

  fprintf (stream, "%s", "  cout << \"this is an int: \" << ");
  fprintf (stream, "%d", (record->myint ? record->myint : ""));

instead of:

  fprintf (stream, "%s", "  cout << \"this is an int: \" << ");
  fprintf (stream, "%d", record->myint);

which caused a compilation error.

So, the new patch:
- fixes the original problem (passing non-strings to generate_string())
- adds test cases for types when used with --expand-tabs
- fixes the above issue (using non-string types with outputcvar_gen_class)

(file #27468)

Tim Marston <edam>
Sat 16 Feb 2013 08:28:50 AM UTC, comment #3: 

It would be better to have a specific test for that :)

Lorenzo Bettini <bettini>
Group administrator
Fri 15 Feb 2013 05:41:56 PM UTC, comment #2: 

Hi,

Sorry for the slow response...

Yes, all tests pass.  That is to say, running "make check", "make my-check" and "make check-valgrind" are all OK.

I haven't written a test for this, no.  I can add one though, if you like?

Tim Marston <edam>
Fri 01 Feb 2013 04:30:29 PM UTC, comment #1: 

Thanks Tim for the patch!
Did you check whether the tests still run?
Did you write a test for this issue as well?

cheers
Lorenzo

Lorenzo Bettini <bettini>
Group administrator
Fri 01 Feb 2013 03:18:37 PM UTC, original submission:  

When run with --expand-tabs, skel variables are passed to generate_string() in
the generated code, regardless of their type.  For string variables, this is
fine.  For non-string variables, this causes a compilation error for generated
C++ code, and a runtime segfault for generated C code.

For example, take the following skel file:

  @array_name@[@array_index:int@]

When run like this:

  $ gengen --expand-tabs < skel

The following code is generated:

  void generate_gengen(ostream &stream, unsigned int indent = 0)
  {
    string indent_str (indent, ' ');
    indent = 0;

    generate_string (array_name, stream, indent + indent_str.length ());
    stream << "[";
    generate_string (array_index, stream, indent + indent_str.length ());
    stream << "]";
    stream << "\n";
    stream << indent_str;
  }

You can see array_name and array_index are both passed to generate_string(),
even though generate_string() takes a const string & as its first parameter,
and not an int.  Passing array_index causes a compiler error.

When run like this:

  gengen --expand-tabs --output-format=c < skel

The following code is generated:

  void
  generate_gengen(FILE *stream, struct gengen_gen_struct *record, unsigned int indent)
  {
    char *indent_str;
    unsigned int i;

    indent_str = (char *) malloc (indent + 1);
    for (i = 0; i < indent; ++i)
      indent_str[i] = ' ';
    indent_str[indent] = '\0';
    indent = 0;

    generate_string ((record->array_name ? record->array_name : ""), stream, indent + strlen (indent_str));
    fprintf (stream, "%s", "[");
    generate_string ((record->array_index ? record->array_index : ""), stream, indent + strlen (indent_str));
    fprintf (stream, "%s", "]");
    fprintf (stream, "%s", "\n");
    fprintf (stream, "%s", indent_str);

    free (indent_str);
  }

Again, you can see record->array_name and record->array_index both being passed
to generate_string(), even though generate_string() takes a const char * as its
first paramater, and not an int.  This creates a compiler warning and segfaults
at runtime (the int is treated as a pointer).

I have attached a patch for src/skelstruct_cpp.cc and src/skelstruct_c.cpp that
works, in both cases, as follows:

Previously, the generation logic worked like this:

  if expand-tabs
    send the variable through generate_string()
  else
    just output the variable

The patch changes this to:

  if variable-type is string and expand-tabs
    send the variable through generate_string()
  else
    just output the variable

This should work OK, because non-string type variables (bools and ints) can not
contain newlines anyway, and do not need to be indented, so generate_string()
would have no effect on them.

Tim Marston <edam>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #27468:  patch added by edam (15KiB - application/octet-stream - New patch, as per comment 4)
file #27364:  bug.patch added by edam (2KiB - text/x-patch - Patch to correct the problem)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by bettini (Posted a comment)
  • -email is unavailable- added by edam (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2013-02-16 edam Attached File- Added patch, #27468
    2013-02-01 bettini Assigned toNone bettini
    2013-02-01 edam Attached File- Added bug.patch, #27364

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code