CMakeLists and Clang Sanitization Options
I would like some feedback on a CMakeLists.txt file I created for compiling my project. I have pasted the CMakeLists as well as my source code below. One thing I would specifically appreciate feedback on the sanitization options I have enabled. Are there more I should enable, and/or should I should reduce? I know some specific sanitization groups can't be used with others (according to the clang documentation). Are there others that would be better preferred to use on a first-pass rather than the ones I chose?
Also - the blob feature I am using, I have based on a StackOverflow answer I read - I understand that this doesn't detect new C source files and I'm fine with that, but besides that subtle detail is this an okay practice to follow?
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(FirstProject C)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_STANDARD 99)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Weverything -fsanitize=undefined,integer,implicit-conversion,nullability,address,leak,cfi -flto -fvisibility=default")
FILE(GLOB Sources *.c)
add_executable(${CMAKE_PROJECT_NAME} ${Sources})
target_link_libraries(${CMAKE_PROJECT_NAME} ${CURSES_LIBRARIES})
main.c
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Generous estimate of the maximum number of digits
// https://stackoverflow.com/a/10536254
#define ULL_DIGITS (3 * sizeof(unsigned long long))
#define ERR_MSG_MAX_LENGTH 32
#define NUL ''
#define NUL_SIZE 1
int ask_ull(unsigned long long *result, const char *prompt);
/**
* Prints a prompt then reads an unsigned long long, using ncurses.
* Returns 0 on success. Returns errno on failure, which is set to
* ERANGE, EDOM, or EIO.
*/
int ask_ull(unsigned long long *result, const char *prompt) {
char buf[ULL_DIGITS + NUL_SIZE];
char *endptr;
printw("%s", prompt);
getnstr(buf, ULL_DIGITS);
*result = strtoull(buf, &endptr, 10);
if (errno == ERANGE) {
// Overflow or underflow
return errno;
}
if (endptr == buf || strchr(buf, '-')) {
// Unsuccessful conversion
errno = EDOM;
return errno;
}
while (isspace(*endptr)) endptr++;
if (*endptr) {
// Trailing junk
errno = EIO;
return errno;
}
errno = 0;
return errno;
}
int main(void) {
unsigned long long height, width, length;
height = width = length = 0;
char errmsg[ERR_MSG_MAX_LENGTH];
errmsg[0] = NUL;
initscr();
printw("--- Volume Calculator --n");
if (!ask_ull(&length, "Enter length: ")) {
sscanf(errmsg, "%s", "Unable to scan length");
}
if (!ask_ull(&width, "Enter width: ")) {
sscanf(errmsg, "%s", "Unable to scan width");
}
if (!ask_ull(&height, "Enter height: ")) {
sscanf(errmsg, "%s", "Unable to scan height");
}
if (errmsg[0] != NUL) {
refresh();
endwin();
perror(errmsg);
return errno;
}
unsigned long long volume = length * width * height;
printw("Volume: %llu", volume);
refresh();
getch();
endwin();
}
c compiler cmake
add a comment |
I would like some feedback on a CMakeLists.txt file I created for compiling my project. I have pasted the CMakeLists as well as my source code below. One thing I would specifically appreciate feedback on the sanitization options I have enabled. Are there more I should enable, and/or should I should reduce? I know some specific sanitization groups can't be used with others (according to the clang documentation). Are there others that would be better preferred to use on a first-pass rather than the ones I chose?
Also - the blob feature I am using, I have based on a StackOverflow answer I read - I understand that this doesn't detect new C source files and I'm fine with that, but besides that subtle detail is this an okay practice to follow?
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(FirstProject C)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_STANDARD 99)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Weverything -fsanitize=undefined,integer,implicit-conversion,nullability,address,leak,cfi -flto -fvisibility=default")
FILE(GLOB Sources *.c)
add_executable(${CMAKE_PROJECT_NAME} ${Sources})
target_link_libraries(${CMAKE_PROJECT_NAME} ${CURSES_LIBRARIES})
main.c
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Generous estimate of the maximum number of digits
// https://stackoverflow.com/a/10536254
#define ULL_DIGITS (3 * sizeof(unsigned long long))
#define ERR_MSG_MAX_LENGTH 32
#define NUL ''
#define NUL_SIZE 1
int ask_ull(unsigned long long *result, const char *prompt);
/**
* Prints a prompt then reads an unsigned long long, using ncurses.
* Returns 0 on success. Returns errno on failure, which is set to
* ERANGE, EDOM, or EIO.
*/
int ask_ull(unsigned long long *result, const char *prompt) {
char buf[ULL_DIGITS + NUL_SIZE];
char *endptr;
printw("%s", prompt);
getnstr(buf, ULL_DIGITS);
*result = strtoull(buf, &endptr, 10);
if (errno == ERANGE) {
// Overflow or underflow
return errno;
}
if (endptr == buf || strchr(buf, '-')) {
// Unsuccessful conversion
errno = EDOM;
return errno;
}
while (isspace(*endptr)) endptr++;
if (*endptr) {
// Trailing junk
errno = EIO;
return errno;
}
errno = 0;
return errno;
}
int main(void) {
unsigned long long height, width, length;
height = width = length = 0;
char errmsg[ERR_MSG_MAX_LENGTH];
errmsg[0] = NUL;
initscr();
printw("--- Volume Calculator --n");
if (!ask_ull(&length, "Enter length: ")) {
sscanf(errmsg, "%s", "Unable to scan length");
}
if (!ask_ull(&width, "Enter width: ")) {
sscanf(errmsg, "%s", "Unable to scan width");
}
if (!ask_ull(&height, "Enter height: ")) {
sscanf(errmsg, "%s", "Unable to scan height");
}
if (errmsg[0] != NUL) {
refresh();
endwin();
perror(errmsg);
return errno;
}
unsigned long long volume = length * width * height;
printw("Volume: %llu", volume);
refresh();
getch();
endwin();
}
c compiler cmake
add a comment |
I would like some feedback on a CMakeLists.txt file I created for compiling my project. I have pasted the CMakeLists as well as my source code below. One thing I would specifically appreciate feedback on the sanitization options I have enabled. Are there more I should enable, and/or should I should reduce? I know some specific sanitization groups can't be used with others (according to the clang documentation). Are there others that would be better preferred to use on a first-pass rather than the ones I chose?
Also - the blob feature I am using, I have based on a StackOverflow answer I read - I understand that this doesn't detect new C source files and I'm fine with that, but besides that subtle detail is this an okay practice to follow?
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(FirstProject C)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_STANDARD 99)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Weverything -fsanitize=undefined,integer,implicit-conversion,nullability,address,leak,cfi -flto -fvisibility=default")
FILE(GLOB Sources *.c)
add_executable(${CMAKE_PROJECT_NAME} ${Sources})
target_link_libraries(${CMAKE_PROJECT_NAME} ${CURSES_LIBRARIES})
main.c
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Generous estimate of the maximum number of digits
// https://stackoverflow.com/a/10536254
#define ULL_DIGITS (3 * sizeof(unsigned long long))
#define ERR_MSG_MAX_LENGTH 32
#define NUL ''
#define NUL_SIZE 1
int ask_ull(unsigned long long *result, const char *prompt);
/**
* Prints a prompt then reads an unsigned long long, using ncurses.
* Returns 0 on success. Returns errno on failure, which is set to
* ERANGE, EDOM, or EIO.
*/
int ask_ull(unsigned long long *result, const char *prompt) {
char buf[ULL_DIGITS + NUL_SIZE];
char *endptr;
printw("%s", prompt);
getnstr(buf, ULL_DIGITS);
*result = strtoull(buf, &endptr, 10);
if (errno == ERANGE) {
// Overflow or underflow
return errno;
}
if (endptr == buf || strchr(buf, '-')) {
// Unsuccessful conversion
errno = EDOM;
return errno;
}
while (isspace(*endptr)) endptr++;
if (*endptr) {
// Trailing junk
errno = EIO;
return errno;
}
errno = 0;
return errno;
}
int main(void) {
unsigned long long height, width, length;
height = width = length = 0;
char errmsg[ERR_MSG_MAX_LENGTH];
errmsg[0] = NUL;
initscr();
printw("--- Volume Calculator --n");
if (!ask_ull(&length, "Enter length: ")) {
sscanf(errmsg, "%s", "Unable to scan length");
}
if (!ask_ull(&width, "Enter width: ")) {
sscanf(errmsg, "%s", "Unable to scan width");
}
if (!ask_ull(&height, "Enter height: ")) {
sscanf(errmsg, "%s", "Unable to scan height");
}
if (errmsg[0] != NUL) {
refresh();
endwin();
perror(errmsg);
return errno;
}
unsigned long long volume = length * width * height;
printw("Volume: %llu", volume);
refresh();
getch();
endwin();
}
c compiler cmake
I would like some feedback on a CMakeLists.txt file I created for compiling my project. I have pasted the CMakeLists as well as my source code below. One thing I would specifically appreciate feedback on the sanitization options I have enabled. Are there more I should enable, and/or should I should reduce? I know some specific sanitization groups can't be used with others (according to the clang documentation). Are there others that would be better preferred to use on a first-pass rather than the ones I chose?
Also - the blob feature I am using, I have based on a StackOverflow answer I read - I understand that this doesn't detect new C source files and I'm fine with that, but besides that subtle detail is this an okay practice to follow?
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(FirstProject C)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_STANDARD 99)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Weverything -fsanitize=undefined,integer,implicit-conversion,nullability,address,leak,cfi -flto -fvisibility=default")
FILE(GLOB Sources *.c)
add_executable(${CMAKE_PROJECT_NAME} ${Sources})
target_link_libraries(${CMAKE_PROJECT_NAME} ${CURSES_LIBRARIES})
main.c
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Generous estimate of the maximum number of digits
// https://stackoverflow.com/a/10536254
#define ULL_DIGITS (3 * sizeof(unsigned long long))
#define ERR_MSG_MAX_LENGTH 32
#define NUL ''
#define NUL_SIZE 1
int ask_ull(unsigned long long *result, const char *prompt);
/**
* Prints a prompt then reads an unsigned long long, using ncurses.
* Returns 0 on success. Returns errno on failure, which is set to
* ERANGE, EDOM, or EIO.
*/
int ask_ull(unsigned long long *result, const char *prompt) {
char buf[ULL_DIGITS + NUL_SIZE];
char *endptr;
printw("%s", prompt);
getnstr(buf, ULL_DIGITS);
*result = strtoull(buf, &endptr, 10);
if (errno == ERANGE) {
// Overflow or underflow
return errno;
}
if (endptr == buf || strchr(buf, '-')) {
// Unsuccessful conversion
errno = EDOM;
return errno;
}
while (isspace(*endptr)) endptr++;
if (*endptr) {
// Trailing junk
errno = EIO;
return errno;
}
errno = 0;
return errno;
}
int main(void) {
unsigned long long height, width, length;
height = width = length = 0;
char errmsg[ERR_MSG_MAX_LENGTH];
errmsg[0] = NUL;
initscr();
printw("--- Volume Calculator --n");
if (!ask_ull(&length, "Enter length: ")) {
sscanf(errmsg, "%s", "Unable to scan length");
}
if (!ask_ull(&width, "Enter width: ")) {
sscanf(errmsg, "%s", "Unable to scan width");
}
if (!ask_ull(&height, "Enter height: ")) {
sscanf(errmsg, "%s", "Unable to scan height");
}
if (errmsg[0] != NUL) {
refresh();
endwin();
perror(errmsg);
return errno;
}
unsigned long long volume = length * width * height;
printw("Volume: %llu", volume);
refresh();
getch();
endwin();
}
c compiler cmake
c compiler cmake
asked 5 mins ago
Faraz
283110
283110
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210342%2fcmakelists-and-clang-sanitization-options%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210342%2fcmakelists-and-clang-sanitization-options%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown