C and C++ compatibility issue

Discussion in 'C++' started by Prashant, Jul 20, 2004.

  1. Prashant

    Prashant Guest

    Hi, i'm having an issue with combining functions from C and C++. I'm
    trying to create a logging function that displays the file, function
    and line number of the code that wants to log a certain message. The
    function is called MainDisplay and outputs to a file called mylog.log.
    For some reason, I SOMETIMES get a segmentation fault when I try and
    open an output stream to the file. The code looks something like
    this:

    //file display.h
    #include <stdarg.h>
    #include <stdio.h>
    #include <iostream>
    #include <fstream.h>
    using namespace std;

    #define MainDisplay(msg, ...) Show(__FILE__,\
    __PRETTY_FUNCTION__, \
    __LINE__, msg, ## __VA_ARGS___);


    void Show(const char* file,
    const char* fcn,
    int line,
    char* display, ...) {

    va_list va;
    va_start(va, display);

    char display2[256];
    vsprintf(display2, display, va);
    va_end(va);

    char linestr[10];
    sprintf(linestr, "%d", line);

    string s = file;
    s += ": function ";
    s += fcn;
    s += ": line ";
    s += linestr;
    s += ": ";
    s += display2;

    log_stream.open("my_log.log", ios::app);
    log_stream << s;
    log_stream.close();
    }

    //end display.h

    The above would be called from any file as follows:

    //file randomfile.cpp

    #include "display.h"

    void randomfunction() {
    char* name = "bob";
    MainDisplay("Hi, my name is %s\n", name);
    }

    //end randomfile.cpp

    But, sometimes, and not always, I get a segmentation fault when I try
    and do
    log_stream.open("my_log.log", ios::app);
    From debugging i've found that this happens if i'm calling MainDisplay
    from a function that does a lot of C-style string manipulation.

    Whats going on here?
     
    Prashant, Jul 20, 2004
    #1
    1. Advertising

  2. * Prashant:
    > The code looks something like this:


    It doesn't.

    Anyway, don't use "..." for variable number of arguments.

    In particular it is UB with respect to non-POD C++ objects,
    but it's a nasty hack anyway and the root cause of the fault.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 20, 2004
    #2
    1. Advertising

  3. [snips]

    On Tue, 20 Jul 2004 15:03:12 -0700, Prashant wrote:

    > //file display.h
    > #include <stdarg.h>
    > #include <stdio.h>
    > #include <iostream>
    > #include <fstream.h>
    > using namespace std;


    Okay, step one: pick a language. <stdio.h> suggests you're using C here,
    <iostream> suggests you're using C++ and <fstream.h> suggests you're using
    something else entirely - Delphi? Java? I don't know, but that header is
    neither C nor C++, so it's impossible to even attempt to sort out your
    problem since we don't even know what language you're using, let alone its
    particular requirements, oddities, etc.
     
    Kelsey Bjarnason, Jul 20, 2004
    #3
  4. Prashant

    David Hilsee Guest

    "Prashant" <> wrote in message
    news:...
    > Hi, i'm having an issue with combining functions from C and C++. I'm
    > trying to create a logging function that displays the file, function
    > and line number of the code that wants to log a certain message. The
    > function is called MainDisplay and outputs to a file called mylog.log.
    > For some reason, I SOMETIMES get a segmentation fault when I try and
    > open an output stream to the file. The code looks something like
    > this:

    <snip>
    > But, sometimes, and not always, I get a segmentation fault when I try
    > and do
    > log_stream.open("my_log.log", ios::app);
    > From debugging i've found that this happens if i'm calling MainDisplay
    > from a function that does a lot of C-style string manipulation.
    >
    > Whats going on here?


    The code you posted uses legacy C functions that are considered dangerous by
    most C and C++ programmers (vsprintf and sprintf). They are known to be
    unsafe because they have no way of confining their results to the array
    passed. If you use them, you may accidentally corrupt your application's
    state. This could be causing your problem, but it's hard to determine if
    that is the culprit since the code is incomplete and possibly not even the
    code that you're using.

    In any situation, it would be a good idea to stop using them. I see three
    reasonable options for eliminating these calls:

    a) Implement a typesafe interface (not varargs and a format string) to the
    Show function. Templates might be an option. This way, you can use the
    typesafe stream operators on log_stream. No temporary buffer is needed for
    this option.

    b) Use a FILE * and vfprintf to write to the file. No temporary buffer is
    needed for this option.

    c) Use the (non-standard in C++) sprintf()-like methods that take a buffer
    size as an argument and will not write past the end of the destination array
    passed.

    Options b) and c) will not protect you from a malformed format string, but
    it's still an improvement.

    BTW, why would you want to open and close the file repeatedly? Why not keep
    it open for the lifetime of the application?

    --
    David Hilsee
     
    David Hilsee, Jul 21, 2004
    #4
  5. Prashant

    Prashant Guest

    Kelsey Bjarnason <> wrote in message news:<>...
    > [snips]
    >
    > On Tue, 20 Jul 2004 15:03:12 -0700, Prashant wrote:
    >
    > > //file display.h
    > > #include <stdarg.h>
    > > #include <stdio.h>
    > > #include <iostream>
    > > #include <fstream.h>
    > > using namespace std;

    >
    > Okay, step one: pick a language. <stdio.h> suggests you're using C here,
    > <iostream> suggests you're using C++ and <fstream.h> suggests you're using
    > something else entirely - Delphi? Java? I don't know, but that header is
    > neither C nor C++, so it's impossible to even attempt to sort out your
    > problem since we don't even know what language you're using, let alone its
    > particular requirements, oddities, etc.


    fstream.h is a C++ library....
    the others are C libraries.
    They're both compatible with each other. Well at least to the extent
    that the compiler is concerned.
    The reason I'm using C here is because I wanted to use the
    __PRETTY_FUNCTION__, __FILE__, and __LINE__ macros. I couldn't find a
    C++ equivalent. I know for sure this works with purely C code without
    any problems.
     
    Prashant, Jul 23, 2004
    #5
  6. Prashant

    Prashant Guest

    "David Hilsee" <> wrote in message news:<>...
    > "Prashant" <> wrote in message
    > news:...
    > > Hi, i'm having an issue with combining functions from C and C++. I'm
    > > trying to create a logging function that displays the file, function
    > > and line number of the code that wants to log a certain message. The
    > > function is called MainDisplay and outputs to a file called mylog.log.
    > > For some reason, I SOMETIMES get a segmentation fault when I try and
    > > open an output stream to the file. The code looks something like
    > > this:

    > <snip>
    > > But, sometimes, and not always, I get a segmentation fault when I try
    > > and do
    > > log_stream.open("my_log.log", ios::app);
    > > From debugging i've found that this happens if i'm calling MainDisplay
    > > from a function that does a lot of C-style string manipulation.
    > >
    > > Whats going on here?

    >
    > The code you posted uses legacy C functions that are considered dangerous by
    > most C and C++ programmers (vsprintf and sprintf). They are known to be
    > unsafe because they have no way of confining their results to the array
    > passed. If you use them, you may accidentally corrupt your application's
    > state. This could be causing your problem, but it's hard to determine if
    > that is the culprit since the code is incomplete and possibly not even the
    > code that you're using.
    >
    > In any situation, it would be a good idea to stop using them. I see three
    > reasonable options for eliminating these calls:
    >
    > a) Implement a typesafe interface (not varargs and a format string) to the
    > Show function. Templates might be an option. This way, you can use the
    > typesafe stream operators on log_stream. No temporary buffer is needed for
    > this option.
    >
    > b) Use a FILE * and vfprintf to write to the file. No temporary buffer is
    > needed for this option.
    >
    > c) Use the (non-standard in C++) sprintf()-like methods that take a buffer
    > size as an argument and will not write past the end of the destination array
    > passed.
    >
    > Options b) and c) will not protect you from a malformed format string, but
    > it's still an improvement.
    >
    > BTW, why would you want to open and close the file repeatedly? Why not keep
    > it open for the lifetime of the application?


    I want to avoid using C code altogether if possible. I just want to
    be able to get the information that can be provided by
    __PRETTY_FUNCTION__, __LINE__ and __FILE__. Are these still safe to
    use in C++?
    I was opening and closing the file repeatedly because for some reason
    it wasn't writing to it if I kept it open the whole time. This is a
    continuously running application, and I want to be able to read the
    file at anytime without having to stop the app.
    And no, this isn't the actual code I was using. This is just a
    shorter version that I posted up because the actual code has a lot of
    error checking and file parsing that I omitted. But I isolated the
    problem to this segment of code, and posted it up just as an example.
    If you want I can post the entire file.
     
    Prashant, Jul 23, 2004
    #6
  7. Prashant

    Default User Guest

    Prashant wrote:
    >
    > Kelsey Bjarnason <> wrote in message news:<>...
    > > [snips]
    > >
    > > On Tue, 20 Jul 2004 15:03:12 -0700, Prashant wrote:
    > >
    > > > //file display.h
    > > > #include <stdarg.h>
    > > > #include <stdio.h>
    > > > #include <iostream>
    > > > #include <fstream.h>
    > > > using namespace std;

    > >
    > > Okay, step one: pick a language. <stdio.h> suggests you're using C here,
    > > <iostream> suggests you're using C++ and <fstream.h> suggests you're using
    > > something else entirely - Delphi? Java? I don't know, but that header is
    > > neither C nor C++, so it's impossible to even attempt to sort out your
    > > problem since we don't even know what language you're using, let alone its
    > > particular requirements, oddities, etc.

    >
    > fstream.h is a C++ library....


    No, it's not. It's not anything in C++. <fstream> (no .h) is a HEADER in
    C++.

    > the others are C libraries.


    C headers, not libraries. They are (most importantly) also C++ headers,
    although deprecated.


    > They're both compatible with each other. Well at least to the extent
    > that the compiler is concerned.
    > The reason I'm using C here is because I wanted to use the
    > __PRETTY_FUNCTION__,


    This is gcc extension, not a C construct.

    __FILE__, and __LINE__ macros. I couldn't find a
    > C++ equivalent.


    Those are part of C++ as well.

    What you were missing is that people were saying to use the new C++ form
    of those headers:

    <cstdarg>
    <cstdio>


    Brian Rodenborn

    Brian Rodenborn
     
    Default User, Jul 23, 2004
    #7
  8. Prashant

    Prashant Guest

    Default User <> wrote in message news:<>...

    > What you were missing is that people were saying to use the new C++ form
    > of those headers:
    >
    > <cstdarg>
    > <cstdio>


    Okay I guess I missed that. Anyway, it makes no difference, I still
    have the same problems. The documentation says that all cstdarg does
    is this:

    namespace std {
    #include <stdarg.h>
    }

    same with <cstdio>
     
    Prashant, Jul 26, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    2
    Views:
    600
  2. gtp1013
    Replies:
    3
    Views:
    588
    dorayme
    Apr 12, 2006
  3. Adi
    Replies:
    2
    Views:
    295
  4. D. Susman
    Replies:
    1
    Views:
    310
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Sep 18, 2007
  5. bruno
    Replies:
    1
    Views:
    283
    Carl Banks
    Apr 30, 2009
Loading...

Share This Page