Skip to content

Commit 1c31004

Browse files
committed
exit(1) on test failures in --repltest mode
add more exit_or_restart lines where appropriate
1 parent 2e15200 commit 1c31004

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

main.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#include "shims.h"
4747

4848
const char* NAME = "cliffi";
49-
const char* VERSION = "v1.10.19";
49+
const char* VERSION = "v1.10.20";
5050
const char* BASIC_USAGE_STRING = "<library> <return_typeflag> <function_name> [[-typeflag] <arg>.. [ ... <varargs>..] ]\n";
5151

5252
bool isTestEnvExit1OnFail = false;
@@ -278,6 +278,7 @@ void parsePrintVariable(char* varName) {
278278
ArgInfo* arg = getVar(varName);
279279
if (arg == NULL) {
280280
fprintf(stderr, "Error printing var: Variable %s not found.\n", varName);
281+
exit_or_restart(1);
281282
} else {
282283
printVariableWithArgInfo(varName, arg);
283284
}
@@ -299,7 +300,7 @@ void parseStoreToMemoryWithAddressAndValue(char* addressStr, int varValueCount,
299300
int args_used = 0;
300301
ArgInfo* arg = parse_one_arg(varValueCount, varValues, &args_used, false);
301302
if (args_used + 1 != varValueCount) {
302-
fprintf(stderr, "Invalid variable value.\n");
303+
fprintf(stderr, "Invalid variable value. Parser failed to consume entire line.\n");
303304
free(arg);
304305
exit_or_restart(1);
305306
return;
@@ -413,13 +414,14 @@ void executeREPLCommand(char* command) {
413414
char** argv;
414415
if (tokenize(command, &argc, &argv) != 0) {
415416
fprintf(stderr, "Error: Tokenization failed for command\n");
416-
return;
417+
exit_or_restart(1);
417418
}
418419

419420
// syntactic sugar for set <var> <value> and print <var>
420421
if (argc == 1) {
421422
if (isHexFormat(argv[0])) {
422423
fprintf(stderr, "You can't print a memory address with specifying a type, try again with: dump <type> %s\n", argv[0]);
424+
exit_or_restart(1);
423425
} else {
424426
parsePrintVariable(argv[0]);
425427
}
@@ -449,6 +451,7 @@ void executeREPLCommand(char* command) {
449451
int invoke_result = invoke_and_print_return_value(call_info, func);
450452
if (invoke_result != 0) {
451453
fprintf(stderr, "Error: Function invocation failed\n");
454+
exit_or_restart(1);
452455
}
453456
}
454457

@@ -483,6 +486,7 @@ void parseSetVariable(char* varCommand) {
483486
// <var> <value>
484487
if (argc < 2) {
485488
fprintf(stderr, "Error: Invalid number of arguments for set\n");
489+
exit_or_restart(1);
486490
return;
487491
}
488492
char* varName = argv[0]; // first argument is the variable name
@@ -499,6 +503,7 @@ void parseStoreToMemory(char* memCommand) {
499503
// <address> <value>
500504
if (argc < 2) {
501505
fprintf(stderr, "Error: Invalid number of arguments for storemem\n");
506+
exit_or_restart(1);
502507
return;
503508
}
504509
char* address = argv[0]; // first argument is the address
@@ -515,6 +520,7 @@ void parseDumpMemory(char* memCommand) {
515520
// <type> <address>
516521
if (argc < 2) {
517522
fprintf(stderr, "Error: Invalid number of arguments for dumpmem\n");
523+
exit_or_restart(1);
518524
return;
519525
}
520526
char* address = argv[argc - 1]; // last argument is the address
@@ -530,6 +536,7 @@ void parseLoadMemoryToVar(char* loadCommand) {
530536
// <var> <type> <address>
531537
if (argc < 3) {
532538
fprintf(stderr, "Error: Invalid number of arguments for loadmem\n");
539+
exit_or_restart(1);
533540
return;
534541
}
535542
char* varName = argv[0];
@@ -549,6 +556,7 @@ void parseCalculateOffset(char* calculateCommand) {
549556
// <var> <library> <symbol> <address>
550557
if (argc < 4) {
551558
fprintf(stderr, "Error: Invalid number of arguments for calculate_offset\n");
559+
exit_or_restart(1);
552560
return;
553561
}
554562
char* varName = argv[0];
@@ -566,6 +574,7 @@ void parseCalculateOffset(char* calculateCommand) {
566574
#endif
567575
if (symbol_address < (uintptr_t)address) {
568576
fprintf(stderr, "Error: Calculated offset is negative. This is likely an error and the variable probably won't work.\n");
577+
exit_or_restart(1);
569578
}
570579
ptrdiff_t offset = symbol_address - (uintptr_t)address; // maybe technically we should use ptrdiff_t instead but it's unlikely that the offset would be negative
571580
printf("Calculation: dlsym(%s,%s)=%p; %p - %p = %p\n", libraryName, symbolName, symbol_handle, symbol_handle, address, (void*)offset);
@@ -585,13 +594,15 @@ void parseHexdump(char* hexdumpCommand) {
585594
// <address> <size>
586595
if (argc < 2) {
587596
fprintf(stderr, "Error: Invalid number of arguments for hexdump\n");
597+
exit_or_restart(1);
588598
return;
589599
}
590600
char* addressStr = argv[0];
591601
char* sizeStr = argv[1];
592602
void* address = getAddressFromAddressStringOrNameOfCoercableVariable(addressStr);
593603
if (address==NULL) {
594604
fprintf(stderr, "Error: Invalid address for hexdump\n");
605+
exit_or_restart(1);
595606
return;
596607
}
597608
size_t size = strtoul(sizeStr, NULL, 0);

0 commit comments

Comments
 (0)