diff --git a/vm/debug.cpp b/vm/debug.cpp index bb3a8b0ce5..914743fc67 100755 --- a/vm/debug.cpp +++ b/vm/debug.cpp @@ -377,9 +377,10 @@ void factor_vm::factorbug() char cmd[1024]; std::cout << "READY\n"; - fflush(stdout); + std::cout.flush(); - if(scanf("%1000s",cmd) <= 0) + std::cin >> std::setw(1024) >> cmd >> std::setw(0); + if(!std::cin.good()) { if(!seen_command) { @@ -402,7 +403,10 @@ void factor_vm::factorbug() if(strcmp(cmd,"d") == 0) { cell addr = read_cell_hex(); - if(scanf(" ") < 0) break; + if (std::cin.peek() == ' ') + std::cin.ignore(); + + if(!std::cin.good()) break; cell count = read_cell_hex(); dump_memory(addr,addr+count); } diff --git a/vm/master.hpp b/vm/master.hpp index b8ababeb2d..d2b9a5f709 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -20,6 +20,7 @@ #include #include #include +#include /* C++ headers */ #include @@ -27,6 +28,7 @@ #include #include #include +#include #define FACTOR_STRINGIZE(x) #x diff --git a/vm/os-macosx.mm b/vm/os-macosx.mm index c5377be8ef..93b4edd06a 100644 --- a/vm/os-macosx.mm +++ b/vm/os-macosx.mm @@ -17,7 +17,7 @@ void early_init(void) Gestalt(gestaltSystemVersion,&version); if(version < 0x1050) { - printf("Factor requires Mac OS X 10.5 or later.\n"); + std::cout << "Factor requires Mac OS X 10.5 or later.\n"; exit(1); } diff --git a/vm/os-unix.hpp b/vm/os-unix.hpp index 54e9d068ef..c774707a76 100644 --- a/vm/os-unix.hpp +++ b/vm/os-unix.hpp @@ -27,8 +27,6 @@ typedef char symbol_char; #define FTELL ftello #define FSEEK fseeko -#define CELL_HEX_FORMAT "%lx" - #define OPEN_READ(path) fopen(path,"rb") #define OPEN_WRITE(path) fopen(path,"wb") diff --git a/vm/os-windows.cpp b/vm/os-windows.cpp index 1ff1b174b5..67f48bc99e 100755 --- a/vm/os-windows.cpp +++ b/vm/os-windows.cpp @@ -57,7 +57,10 @@ BOOL factor_vm::windows_stat(vm_char *path) void factor_vm::windows_image_path(vm_char *full_path, vm_char *temp_path, unsigned int length) { - SNWPRINTF(temp_path, length-1, L"%s.image", full_path); + wcsncpy(temp_path, full_path, length - 1); + unsigned full_path_len = wcslen(full_path); + if (full_path_len < length - 1) + wcsncat(temp_path, L".image", length - full_path_len - 1); temp_path[length - 1] = 0; } @@ -74,7 +77,10 @@ const vm_char *factor_vm::default_image_path() if((ptr = wcsrchr(full_path, '.'))) *ptr = 0; - SNWPRINTF(temp_path, MAX_UNICODE_PATH-1, L"%s.image", full_path); + wcsncpy(temp_path, full_path, MAX_UNICODE_PATH - 1); + unsigned full_path_len = wcslen(full_path); + if (full_path_len < length - 1) + wcsncat(temp_path, L".image", MAX_UNICODE_PATH - full_path_len - 1); temp_path[MAX_UNICODE_PATH - 1] = 0; return safe_strdup(temp_path); diff --git a/vm/os-windows.hpp b/vm/os-windows.hpp index ad8a9907a7..03dec4bb02 100755 --- a/vm/os-windows.hpp +++ b/vm/os-windows.hpp @@ -23,18 +23,10 @@ typedef wchar_t vm_char; #define FTELL ftell #define FSEEK fseek #define SNPRINTF _snprintf - #define SNWPRINTF _snwprintf #else #define FTELL ftello64 #define FSEEK fseeko64 #define SNPRINTF snprintf - #define SNWPRINTF snwprintf -#endif - -#ifdef WIN64 - #define CELL_HEX_FORMAT "%Ix" -#else - #define CELL_HEX_FORMAT "%lx" #endif #define OPEN_READ(path) _wfopen((path),L"rb") diff --git a/vm/run.cpp b/vm/run.cpp index 605fd9b725..00d15fc42a 100755 --- a/vm/run.cpp +++ b/vm/run.cpp @@ -11,7 +11,13 @@ void factor_vm::primitive_exit() void factor_vm::primitive_nano_count() { u64 nanos = nano_count(); - if(nanos < last_nano_count) critical_error("Monotonic counter decreased",0); + if(nanos < last_nano_count) { + std::cout << "Monotonic counter decreased from 0x"; + std::cout << std::hex << last_nano_count; + std::cout << " to 0x" << nanos << "." << std::dec << "\n"; + std::cout << "Please report this error.\n"; + current_vm()->factorbug(); + } last_nano_count = nanos; ctx->push(from_unsigned_8(nanos)); } diff --git a/vm/utilities.cpp b/vm/utilities.cpp index 3e976d0619..11d3de78cc 100755 --- a/vm/utilities.cpp +++ b/vm/utilities.cpp @@ -14,7 +14,8 @@ vm_char *safe_strdup(const vm_char *str) cell read_cell_hex() { cell cell; - if(scanf(CELL_HEX_FORMAT,&cell) < 0) exit(1); + std::cin >> std::hex >> cell >> std::dec; + if(!std::cin.good()) exit(1); return cell; }