Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "Morten W. Petersen" <morphex@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Getting sensible information from gdb |
| Date | 2015-10-12 00:07 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <mvemib$140$1@speranza.aioe.org> (permalink) |
| References | <mvecqu$b25$1@speranza.aioe.org> <tbjl1bt5723tvgrj1fuoub094fo2bis587@4ax.com> |
On 11.10.2015 23:47, Geoff wrote:
> On Sun, 11 Oct 2015 21:21:43 +0200, "Morten W. Petersen"
> <morphex@gmail.com> wrote:
[...]
> Your fault is here, as I have said previously:
>
>> /*
>>
>> The amount of characters to read from a char stream at a time.
>>
>> The amount of bytes we can read.
>>
>> */
>> #define READ_AMOUNT 4096
>> #define READ_BYTES 1024*1024*100
>> #if (READ_BYTES % READ_AMOUNT)
>> #error READ_BYTES must be divisible by READ_AMOUNT
>> #endif
>
> This is unsafe and unnecessary. Large static buffers are not going to
> protect you.
>
> If you _must_ do silly things like this then dispense with the
> cross-check of the sizes and simply write:
>
> #define READ_AMOUNT 4096
> #define READ_BYTES READ_AMOUNT * 100
>
> However, this is still unsafe when handling uncontrolled user input.
> You should have learned those principles in your Python practice.
>
>> #define CHAR_SIZE sizeof(char)
>
>> #define HANDLE_ERROR(message, ...) char* _message = message;\
>> char buffer[1024*1024];\
>> sprintf(buffer, _message, __VA_ARGS__);\
>> printf("Error: %s\n", message)
>
>> #define FAIL(message, ...) char* _message = message;\
>> char buffer__abc[1024*1024];\
>> sprintf(buffer__abc, _message, __VA_ARGS__);\
>> printf("\n%s\n", buffer__abc); fflush(NULL); \
>> exit(1)
>
> These macros use unnecessarily large buffers. Use 1024, not 1024*1024.
> Every invocation of these macros adds to the stack space used by your
> test program, eventually your program smashes its own stack. Your
> program is living up to its name.
>
> The macros themselves are completely unnecessary. Get rid of them.
Ah right, I remember some talk about this and I forgot about it.
> I don't know where you got these macros from but they are not helping
> you at all. This is called cargo cult programming where I come from.
> You're using stuff without a fundamental understanding of why and
> where it should be used.
Or I bump my head into things until I get it. :)
I think most or all of these FAIL and HANDLE_ERROR instances are
conditional, that is they pop up at the end of something if one
shouldn't get to the end (it should return earlier), or they pop
up within an if statement with a following section of code in brackets..
So regardless of where a variable has been defined in a function,
it may be allocated at the very beginning of the function call?
> Your test driver program is just that, a test driver. Write your
> diagnostics output messages as simple printf() statements and be done
> with it. Every test in your driver code should evaluate the returns
> from your library functions and only when the return is invalid per
> the test, terminate or issue a message. Your library should never
> explicitly terminate for any reason if you are going to meet your
> stated design goals.
Mm, OK.
> If you insist on using FAIL-type functionality learn to use assert()
> instead. Your FAIL macro is interfering with the proper functioning of
> your library code. Even assert would be wrong for many of your
> functions. What you should be doing is coding your library to return
> errors to the calling function if and only if they can fail. You
> should always write your callers to check the return values from your
> library functions and only then should the caller exit the program,
> your library should NEVER terminate the program.
>
> Every FAIL call is a failure on your part to properly plan and code
> your library functions to handle errors properly.
>
> Speaking of checking return values, you need to learn to handle the C
> library functions properly. You fail to check the return value of
> malloc(), realloc() and fopen().
OK. Yes that's true, I should get some handling in there for failing
memory allocations. I was thinking of having a function that could
reorganize/pack the memory allocations.
As for fopen, I'm thinking the design should go towards dealing with
streams, so the file will already be open. And the fopen in the test
suite, if it fails, will fail the test and that will be an indication
of failure in that part.
> You needlessly initialize your buffer and file pointers to NULL, then
> you fail to check that malloc and fopen returned valid pointers. If
> they are going to fail, they are going to be NULL anyway, but then you
> proceed to use them without checking them first.
>
> Write:
>
> unicode_char *buffer = NULL; // needless initialization
> source_buffer_index read = 0;
> small_fast_int valid_unicode = 0;
>
> buffer = (unicode_char*)malloc(READ_BYTES);
> if (!buffer) {
> printf("ERROR: buffer allocation failed\n");
> exit(1);
> }
>
> FILE *file = NULL; // another needless initialization
> file = fopen("test.xml", "rb+");
> if (file == NULL) {
> printf("ERROR: XML file not opened\n");
> exit(1);
> }
My thinking was that I should never leave a pointer uninitialized,
so I'll switch to declaring and initializing to a value in the same
go.
-Morten
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-11 21:21 +0200
Re: Getting sensible information from gdb Geoff <geoff@invalid.invalid> - 2015-10-11 14:47 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 00:07 +0200
Re: Getting sensible information from gdb Geoff <geoff@invalid.invalid> - 2015-10-11 16:15 -0700
Re: Getting sensible information from gdb James Kuyper <jameskuyper@verizon.net> - 2015-10-11 21:02 -0400
Re: Getting sensible information from gdb Geoff <geoff@invalid.invalid> - 2015-10-11 18:55 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 04:04 +0200
Re: Getting sensible information from gdb Geoff <geoff@invalid.invalid> - 2015-10-11 19:37 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 05:16 +0200
Re: Getting sensible information from gdb James Kuyper <jameskuyper@verizon.net> - 2015-10-12 00:01 -0400
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 06:06 +0200
Re: Getting sensible information from gdb James Kuyper <jameskuyper@verizon.net> - 2015-10-12 00:48 -0400
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 07:08 +0200
Re: Getting sensible information from gdb Keith Thompson <kst-u@mib.org> - 2015-10-11 18:56 -0700
Re: Getting sensible information from gdb Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-11 18:14 -0400
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 00:20 +0200
Re: Getting sensible information from gdb James Kuyper <jameskuyper@verizon.net> - 2015-10-11 21:07 -0400
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 03:59 +0200
Re: Getting sensible information from gdb Geoff <geoff@invalid.invalid> - 2015-10-11 23:21 -0700
Re: Getting sensible information from gdb Geoff <geoff@invalid.invalid> - 2015-10-11 23:32 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 08:42 +0200
Re: Getting sensible information from gdb Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-12 17:13 +0100
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 23:50 +0200
Re: Getting sensible information from gdb Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-13 01:39 +0100
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-13 03:04 +0200
Re: Getting sensible information from gdb Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-13 14:13 +0100
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-13 16:26 +0200
Re: Getting sensible information from gdb Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-13 17:16 +0100
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-13 18:27 +0200
Re: Getting sensible information from gdb Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 00:17 +0100
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-14 02:26 +0200
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-14 03:59 +0200
Re: Getting sensible information from gdb Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 13:04 +0100
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-15 12:48 +0200
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 08:41 +0200
Re: Getting sensible information from gdb Geoff <geoff@invalid.invalid> - 2015-10-11 16:18 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 01:22 +0200
Re: Getting sensible information from gdb Geoff <geoff@invalid.invalid> - 2015-10-11 16:42 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 02:16 +0200
Getting sensible information from gdb "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-11 18:59 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 04:09 +0200
Re: Getting sensible information from gdb "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-11 19:28 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 04:59 +0200
Re: Getting sensible information from gdb "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-12 07:10 -0700
Re: Getting sensible information from gdb Rosario19 <Ros@invalid.invalid> - 2015-10-12 19:41 +0200
Re: Getting sensible information from gdb Ian Collins <ian-news@hotmail.com> - 2015-10-12 17:59 +1300
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 07:36 +0200
Re: Getting sensible information from gdb Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 22:59 -0700
Re: Getting sensible information from gdb "Morten W. Petersen" <morphex@gmail.com> - 2015-10-12 08:27 +0200
Re: Getting sensible information from gdb Johann Klammer <klammerj@NOSPAM.a1.net> - 2015-10-12 10:55 +0200
Re: Getting sensible information from gdb Keith Thompson <kst-u@mib.org> - 2015-10-12 08:04 -0700
Re: Getting sensible information from gdb Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-12 17:35 +0100
Re: Getting sensible information from gdb Keith Thompson <kst-u@mib.org> - 2015-10-12 08:00 -0700
Re: Getting sensible information from gdb James Kuyper <jameskuyper@verizon.net> - 2015-10-12 12:25 -0400
csiph-web