Skip to content

Rework memory error handling and enable debug mode by default#115

Closed
cgzones wants to merge 1 commit intohtop-dev:masterfrom
cgzones:failures
Closed

Rework memory error handling and enable debug mode by default#115
cgzones wants to merge 1 commit intohtop-dev:masterfrom
cgzones:failures

Conversation

@cgzones
Copy link
Copy Markdown
Member

@cgzones cgzones commented Sep 11, 2020

Print exact location on a memory allocation or write (snprintf) failure,
instead of just exiting with: htop: Success

Enable asserts by not using -DNDEBUG by default.
Let users/package maintainers specify it if they want to.

Closes: #112

@BenBE
Copy link
Copy Markdown
Member

BenBE commented Sep 11, 2020

LTGM so far …

Copy link
Copy Markdown
Member

@fasterit fasterit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you remove the inlines? Have you checked on the performance impact?

I'm not sure we want to invert the debug logic as dozens of distros need to adjust their packaging then and - typically - if you run make on what you just grabbed from git, you'd not expect a debug build either.

@cgzones cgzones added code quality ♻️ Code quality enhancement needs-discussion 🤔 Changes need to be discussed and require consent labels Sep 12, 2020
@cgzones
Copy link
Copy Markdown
Member Author

cgzones commented Sep 12, 2020

The inline specifier is quite useless. Compilers are free to inline any function they like and to not inline any function they don't like, whether the function is annotated with inline or not.
The specifier needs to be dropped to allow the assert statements inside.

Regarding NDEBUG: For example systemd gets build on Debian without it and they use asserts heavily.

@fasterit
Copy link
Copy Markdown
Member

The inline specifier is quite useless. Compilers are free to inline any function they like and to not inline any function they don't like, whether the function is annotated with inline or not.

It tells the compiler you want it inlined and sane compilers will obey.

The specifier needs to be dropped to allow the assert statements inside.

Similar to the -pedantic above, you drop standards in the code base to modernize it just in the place that you touch.

I think this needs a more general approach to either stay with the current style / language level or go beyond that to some defined level.

Regarding NDEBUG: For example systemd gets build on Debian without it and they use asserts heavily.

I do not see why that is an argument for you to invert the logic of the code base and put work on every downstream packager.

@BenBE
Copy link
Copy Markdown
Member

BenBE commented Sep 13, 2020

Unless there's a really good reason to do so (and I currently see none) I'd prefer to avoid any GNU extensions and thus keep -pedantic.

For the -DNDEBUG: I'm okay with dropping that define, as this makes some potential issues more visible. And with the few asserts in the code base this shouldn't change too much overall either, even if downstream didn't re-add those for their builds.

XAlloc.h Outdated
#endif
#define xAsprintf(strp, fmt, ...) \
do { \
int _ret = asprintf(strp, fmt, __VA_ARGS__); \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer vasprintf as shown with my implementation in #155.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need inside a macro for manual argument unpacking

XAlloc.h Outdated
} while(0)

char* xStrdup_(const char* str) ATTR_NONNULL;
#define xSnprintf(fmt, len, ...) \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro's signature is wrong. Cf. #155.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero sized variable arguments are a C extension, so #define xSnprintf(buf, len, fmt, ...) does not work in case of xSnprintf(foo, 42, "Hello World");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not what #155 uses (partly for that reason).
Also, the current macro calls the first argument fmt, while it should be buf.
BTW: That naming issue was what led me to rewrite the macro as a proper function.

Print exact location on a memory allocation or write (snprintf) failure,
instead of just exiting with: `htop: Success`

    Failed to write memory (5/7) [htop.c:211]
    Aborted
@cgzones
Copy link
Copy Markdown
Member Author

cgzones commented Oct 7, 2020

Superseded by #204

@cgzones cgzones closed this Oct 7, 2020
@cgzones cgzones deleted the failures branch October 7, 2020 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code quality ♻️ Code quality enhancement needs-discussion 🤔 Changes need to be discussed and require consent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quiet failure on xSnprintf failure

3 participants