【发布时间】:2016-06-01 11:03:57
【问题描述】:
我在code review 上发布了我的代码,得到的答案是我没有进行错误检查。 AFAIK 我可以通过多种方式进行错误检查:
- 前置条件和后置条件的断言
-
perror用法或类似的,我有时会这样做,但我还没学过 - 使用来自函数或类似函数的布尔返回值进行手动手动编码错误检查,指示调用成功或失败
是否有更多方法或推荐的特定方法?同时使用perror 和断言是好还是坏?代码在my github 中,main 循环如下所示:
int main(int argc, char *argv[]) {
bool donotrun = false;
struct sigaction new_action, old_action;
hashtable_t *hashtable = ht_create(65536);
/* Set up the structure to specify the new action. */
new_action.sa_handler = termination_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGINT, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction(SIGINT, &new_action, NULL);
sigaction(SIGHUP, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction(SIGHUP, &new_action, NULL);
sigaction(SIGTERM, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction(SIGTERM, &new_action, NULL);
void *pParser;
char *c;
int index = 0;
int i;
char *cvalue = NULL;
const char *commandFile = "";
bool quietFlag;
while (1) {
index = 0;
i = getopt_long(argc, argv, "pc:vh",
options, &index);
if (i == -1)
break;
switch (i) {
case 'p': {
exit(EXIT_SUCCESS);
}
case 'v': {
printf("sh OpenShell version 0.1(a)\n");
printf("Version: %s\n", VERSION);
exit(EXIT_SUCCESS);
}
case 'h': {
usage();
exit(EXIT_SUCCESS);
}
case 'c': {
cvalue = optarg;
command(cvalue, hashtable);
exit(EXIT_SUCCESS);
}
case 'f':
/*
* Execute commands from file.
* This is used for osh script files.
* The quiet flag is also set.
*/
if ((argc != 1) || commandFile)
usage();
quietFlag = true;
argc--;
break;
case '?':
if (optopt == 'c')
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr,
"Unknown option character `\\x%x'.\n",
optopt);
default: {
return 1;
}
}
}
getPath();
pParser = (void *) ParseAlloc(malloc);
char *copy = "";
for (; ;) {
bool scanning = true;
bool calc = true;
while (scanning) {
char *line = NULL;
line = readline("$ ");
if (line == NULL) {
/* No more lines, so exit the loop. */
break;
}
if (line)
copy = strdup(line);
if (line && !strstr(line, "for") && !strstr(line, "==") && !strstr(line, "if") && strstr(line, "=")) {
donotrun = true;
char str[128];
char *ptr;
strcpy(str, line);
strtok_r (str, "=", &ptr);
ht_set(hashtable, str, ptr);
}
if (!scanning)
break;
if (!isatty(fileno(stdin))) {
*argv++;
readFile(*argv++, hashtable);
free(line);
exit(0);
}
else {
if (!donotrun) {
line = str_replace(line, " | ", "|");
line = str_replace(line, " |", "|");
command(line, hashtable);
}
donotrun = false;
add_history(copy);
}
free(copy);
}
}
// ParseFree(pParser, free);FIXME: where should this go?
return 0;
}
然后我有断言,我必须在开发过程中注释掉,现在我不确定断言应该是什么。
/* Returns a struct that has the number of "chunks" the list of chunks.
* Splits the command by char | and then by whitespace and return a list of struct pointers
*/
struct str_list *list_split(const char *a_str, char *a_delim) {
char **result = 0;
size_t count = 0;
char *tmp = (char *) a_str;
char *ctmp;
char *token = "";
char *last_comma = 0;
char *tmp2 = (char *) a_str; /* TODO: This variable can reuse tmp */
//char *delim[2];
//delim[0] = a_delim;
struct str_list *chunks = NULL;
/* Count how many elements will be extracted. */
while (*tmp) {
if (*a_delim == *tmp) {
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
count++;
result = alloc_argv(count);
char **tmpresult = alloc_argv(count);
chunks = malloc(count * sizeof *chunks);
if (result == NULL) {
printf("Error allocating memory!\n");
return chunks;;
}
if (result) {
size_t idx = 0;
token = strtok((char *) strdup(a_str), "|");
int a = 0;
while (token) {
// assert(idx < count); Why must I turn off assertions?
tmpresult[a] = strdup(token);
a++;
ctmp = strdup(token);
*(result + idx++) = ctmp; /* memory leak! how to free() */;
token = strtok(0, a_delim);
}
// assert(idx == count - 1);
*(result + idx) = 0;
}
chunks->argv = alloc_argv(BUFFER_SIZE);//malloc(chunks->size * sizeof(char *) * 1);
int i = 0;
chunks = tokenize(&i, chunks, result, count, tmp2);
chunks->pipes = i; /* important! to get this right */
free(ctmp);
return expand_shell(tmpresult, chunks);
}
【问题讨论】:
-
等一下,您是在询问检查您使用的东西的错误,还是询问实施此类错误发射并检查您自己开发的东西我>?
-
@MarcusMüller 我想知道为什么我必须注释掉我的断言,我想知道在我编写的代码中应该在哪里使用断言。在某些地方我已经使用
perror,但我不知道细节。 -
perror()不检查错误。它只是将errno的当前值的长文本错误消息记录到stderr。 -
当“发生了非常错误的事情,没有必要继续”时,您会使用断言
-
@user3528438 这实际上是我想知道的。 “我应该在哪里使用断言?”我有断言,重构,然后我不得不注释掉断言,但代码有效。
标签: c error-handling assert