--- cygcheck.cc.orig Fri Dec 1 18:15:20 2000 +++ cygcheck.cc Wed Dec 6 16:53:14 2000 @@ -77,9 +77,24 @@ common_apps[] = int num_paths = 0, max_paths = 0; char **paths = 0; +/* + * keyeprint() is used to report failure modes + */ +int +keyeprint (const char *name) +{ + fprintf (stderr, "cygcheck: %s failed: %lu\n", name, GetLastError ()); + return 1; +} + void add_path (char *s, int maxlen) { + if (NULL == s) + { + keyeprint("add_path(): NULL passed for s"); + return; + } if (num_paths >= max_paths) { max_paths += 10; @@ -89,6 +104,11 @@ add_path (char *s, int maxlen) paths = (char **) malloc (max_paths * sizeof (char *)); } paths[num_paths] = (char *) malloc (maxlen + 1); + if (NULL == paths[num_paths]) + { + keyeprint("add_path(): malloc()"); + return; + } memcpy (paths[num_paths], s, maxlen); paths[num_paths][maxlen] = 0; char *e = paths[num_paths] + strlen (paths[num_paths]); @@ -106,8 +126,14 @@ init_paths () char tmp[4000], *sl; add_path ((char *) ".", 1); /* to be replaced later */ add_path ((char *) ".", 1); /* the current directory */ - GetSystemDirectory (tmp, 4000); - add_path (tmp, strlen (tmp)); + if (0 != GetSystemDirectory (tmp, 4000)) + { + add_path (tmp, strlen (tmp)); + } + else + { + keyeprint("init_paths(): GetSystemDirectory()"); + } sl = strrchr (tmp, '\\'); if (sl) { @@ -146,6 +172,18 @@ find_on_path (char *file, char *default_ static char rv[4000]; char tmp[4000], *ptr = rv; + if (NULL == file) + { + keyeprint("find_on_path(): NULL pointer for file"); + return (0); + } + + if (NULL == default_extension) + { + keyeprint("find_on_path(): NULL pointer for default_extension"); + return (0); + } + if (strchr (file, ':') || strchr (file, '\\') || strchr (file, '/')) return file; @@ -208,8 +246,14 @@ get_word (HANDLE fh, int offset) { short rv; unsigned r; - SetFilePointer (fh, offset, 0, FILE_BEGIN); - ReadFile (fh, &rv, 2, (DWORD *) &r, 0); + + if ((INVALID_SET_FILE_POINTER == SetFilePointer (fh, offset, 0, FILE_BEGIN)) && + (NO_ERROR != GetLastError())) + keyeprint("get_word(): SetFilePointer()"); + + if (0 == ReadFile (fh, &rv, 2, (DWORD *) &r, 0)) + keyeprint("get_word(): Readfile()"); + return rv; } @@ -218,8 +262,14 @@ get_dword (HANDLE fh, int offset) { int rv; unsigned r; - SetFilePointer (fh, offset, 0, FILE_BEGIN); - ReadFile (fh, &rv, 4, (DWORD *) &r, 0); + + if ((INVALID_SET_FILE_POINTER == SetFilePointer (fh, offset, 0, FILE_BEGIN)) && + (NO_ERROR != GetLastError())) + keyeprint("get_word(): SetFilePointer()"); + + if (0 == ReadFile (fh, &rv, 4, (DWORD *) &r, 0)) + keyeprint("get_dword(): Readfile()"); + return rv; } @@ -236,6 +286,13 @@ int rva_to_offset (int rva, char *sections, int nsections, int *sz) { int i; + + if (NULL == sections) + { + keyeprint("rva_to_offset(): NULL passed for sections"); + return(0); + } + for (i = 0; i < nsections; i++) { Section *s = (Section *) (sections + i * 40); @@ -274,14 +331,15 @@ struct ImpDirectory }; -void track_down (char *file, char *suffix, int lvl); +void +track_down (char *file, char *suffix, int lvl); #define CYGPREFIX (sizeof ("%%% Cygwin ") - 1) static void cygwin_info (HANDLE h) { - char *buf, *bufend; - char *major, *minor; + char *buf, *bufend, *free_me = NULL; + char *major, *minor = NULL; const char *hello = " Cygwin DLL version info:\n"; DWORD size = GetFileSize (h, NULL); DWORD n; @@ -290,31 +348,56 @@ cygwin_info (HANDLE h) return; buf = (char *) malloc (size); - if (!buf) - return; + if (NULL == buf) + { + keyeprint("track_down(): malloc()"); + return; + } (void) SetFilePointer (h, 0, NULL, FILE_BEGIN); if (!ReadFile (h, buf, size, &n, NULL)) - return; - + { + free(buf); + return; + } + keyeprint("track_down(): readfile succeeded"); bufend = buf + size; + + /* + * because the initial pointer is modified in this weird way, we assign + * a placeholder so we can free it later. otherwise we introduce a memory leak. + * TODO: clean this up to be more straightforward. + */ + free_me = buf; major = minor = NULL; while (buf < bufend) - if ((buf = (char *) memchr (buf, '%', bufend - buf)) == NULL) - break; - else if (strncmp ("%%% Cygwin ", buf, CYGPREFIX) != 0) - buf++; - else - { - char *p = strchr (buf += CYGPREFIX, '\n'); - fputs (hello, stdout); - fputs (" ", stdout); - fwrite (buf, 1 + p - buf, 1, stdout); - hello = ""; - } - + { + if ((buf = (char *) memchr (buf, '%', bufend - buf)) == NULL) + break; + else if (strncmp ("%%% Cygwin ", buf, CYGPREFIX) != 0) + buf++; + else + { + if ((buf + CYGPREFIX) > bufend) + { + keyeprint("track_down(): buf + CYGPREFIX was larger than bufend"); + break; + } + char *p = strchr (buf += CYGPREFIX, '\n'); + fputs (hello, stdout); + fputs (" ", stdout); + if (NULL == p) + { + fputs("unknown", stdout); + break; + } + fwrite (buf, 1 + p - buf, 1, stdout); + hello = ""; + } + } if (!*hello) puts (""); + free(free_me); return; } @@ -326,13 +409,26 @@ dll_info (const char *path, HANDLE fh, i int pe_header_offset = get_dword (fh, 0x3c); int opthdr_ofs = pe_header_offset + 4 + 20; unsigned short v[6]; - SetFilePointer (fh, opthdr_ofs + 40, 0, FILE_BEGIN); - ReadFile (fh, &v, sizeof (v), &junk, 0); + + if (NULL == path) + { + keyeprint("dll_info(): NULL passed for path"); + return; + } + + if ((INVALID_SET_FILE_POINTER == SetFilePointer (fh, opthdr_ofs + 40, 0, FILE_BEGIN)) && + (NO_ERROR != GetLastError())) + keyeprint("dll_info(): SetFilePointer()"); + + if (0 == ReadFile (fh, &v, sizeof (v), &junk, 0)) + keyeprint("dll_info(): Readfile()"); + if (verbose) printf (" - os=%d.%d img=%d.%d sys=%d.%d\n", v[0], v[1], v[2], v[3], v[4], v[5]); else printf ("\n"); + int num_entries = get_dword (fh, opthdr_ofs + 92); int export_rva = get_dword (fh, opthdr_ofs + 96); int export_size = get_dword (fh, opthdr_ofs + 100); @@ -341,21 +437,33 @@ dll_info (const char *path, HANDLE fh, i int nsections = get_word (fh, pe_header_offset + 4 + 2); char *sections = (char *) malloc (nsections * 40); - SetFilePointer (fh, + + if ((INVALID_SET_FILE_POINTER == SetFilePointer (fh, pe_header_offset + 4 + 20 + get_word (fh, pe_header_offset + 4 + - 16), 0, FILE_BEGIN); - ReadFile (fh, sections, nsections * 40, &junk, 0); + 16), 0, FILE_BEGIN)) && + (NO_ERROR != GetLastError())) + keyeprint("dll_info(): SetFilePointer()"); + + if (0 == ReadFile (fh, sections, nsections * 40, &junk, 0)) + keyeprint("dll_info(): Readfile()"); if (verbose && num_entries >= 1 && export_size > 0) { int expsz; int expbase = rva_to_offset (export_rva, sections, nsections, &expsz); + if (expbase) { - SetFilePointer (fh, expbase, 0, FILE_BEGIN); + if ((INVALID_SET_FILE_POINTER == SetFilePointer (fh, expbase, 0, FILE_BEGIN)) && + (NO_ERROR != GetLastError())) + keyeprint("dll_info(): SetFilePointer()"); + unsigned char *exp = (unsigned char *) malloc (expsz); - ReadFile (fh, exp, expsz, &junk, 0); + + if (0 == ReadFile (fh, exp, expsz, &junk, 0)) + keyeprint("dll_info(): Readfile()"); + ExpDirectory *ed = (ExpDirectory *) exp; int ofs = ed->name_rva - export_rva; struct tm *tm = localtime ((const time_t *) &(ed->timestamp)); @@ -378,9 +486,20 @@ dll_info (const char *path, HANDLE fh, i int impbase = rva_to_offset (import_rva, sections, nsections, &impsz); if (impbase) { - SetFilePointer (fh, impbase, 0, FILE_BEGIN); + if ((INVALID_SET_FILE_POINTER == SetFilePointer (fh, impbase, 0, FILE_BEGIN)) + && (NO_ERROR != GetLastError())) + keyeprint("dll_info(): SetFilePointer()"); + unsigned char *imp = (unsigned char *) malloc (impsz); - ReadFile (fh, imp, impsz, &junk, 0); + if (NULL == imp) + { + keyeprint("dll_info(): malloc()"); + return; + } + + if (0 == ReadFile (fh, imp, impsz, &junk, 0)) + keyeprint("dll_info(): Readfile()"); + ImpDirectory *id = (ImpDirectory *) imp; for (i = 0; id[i].name_rva; i++) { @@ -397,6 +516,18 @@ dll_info (const char *path, HANDLE fh, i void track_down (char *file, char *suffix, int lvl) { + if (NULL == file) + { + keyeprint("track_down(): malloc()"); + return; + } + + if (NULL == suffix) + { + keyeprint("track_down(): malloc()"); + return; + } + char *path = find_on_path (file, suffix, 0, 1); if (!path) { @@ -427,6 +558,8 @@ track_down (char *file, char *suffix, in printf (" (already done)\n"); } return; + default: + break; } if (lvl) @@ -453,23 +586,33 @@ track_down (char *file, char *suffix, in dll_info (path, fh, lvl, 1); d->state = DID_INACTIVE; - CloseHandle (fh); + if (0 == CloseHandle (fh)) + keyeprint("track_down(): CloseHandle()"); } void ls (char *f) { + if (NULL == f) + keyeprint("ls(): NULL passed for f"); + HANDLE h = CreateFile (f, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); BY_HANDLE_FILE_INFORMATION info; - GetFileInformationByHandle (h, &info); + + if (0 == GetFileInformationByHandle (h, &info)) + keyeprint("ls(): GetFileInformationByHandle()"); + SYSTEMTIME systime; - FileTimeToSystemTime (&info.ftLastWriteTime, &systime); + + if (0 == FileTimeToSystemTime (&info.ftLastWriteTime, &systime)) + keyeprint("ls(): FileTimeToSystemTime()"); printf ("%5dk %04d/%02d/%02d %s", (((int) info.nFileSizeLow) + 512) / 1024, systime.wYear, systime.wMonth, systime.wDay, f); dll_info (f, h, 16, 0); - CloseHandle (h); + if (0 == CloseHandle (h)) + keyeprint("ls(): CloseHandle()"); } @@ -551,8 +694,20 @@ scan_registry (RegInfo * prev, HKEY hKey if (cygnus) { show_reg (&ri, 0); + char *value_name = (char *) malloc (max_value_len + 1); + if (NULL == value_name) + { + keyeprint("scan_registry(): malloc()"); + return; + } + char *value_data = (char *) malloc (max_valdata_len + 1); + if (NULL == value_data) + { + keyeprint("scan_registry(): malloc()"); + return; + } for (i = 0; i < num_values; i++) { @@ -593,7 +748,8 @@ scan_registry (RegInfo * prev, HKEY hKey == ERROR_SUCCESS) { scan_registry (&ri, sKey, subkey_name, cygnus); - RegCloseKey (sKey); + if (ERROR_SUCCESS != RegCloseKey (sKey)) + keyeprint("scan_registry(): RegCloseKey()"); } } } @@ -614,7 +770,8 @@ dump_sysinfo () OSVERSIONINFO osversion; osversion.dwOSVersionInfoSize = sizeof (osversion); - GetVersionEx (&osversion); + if (0 == GetVersionEx (&osversion)) + keyeprint("dump_sysinfo(): GetVersionEx()"); char *osname = (char *) "unknown OS"; switch (osversion.dwPlatformId) { @@ -659,9 +816,11 @@ dump_sysinfo () break; s = e + 1; } - - GetSystemDirectory (tmp, 4000); + + if (0 == GetSystemDirectory (tmp, 4000)) + keyeprint("dump_sysinfo(): GetSystemDirectory()"); printf ("\nSysDir: %s\n", tmp); + GetWindowsDirectory (tmp, 4000); printf ("WinDir: %s\n\n", tmp); @@ -757,8 +916,14 @@ dump_sysinfo () DWORD serno = 0, maxnamelen = 0, flags = 0; name[0] = name[0] = fsname[0] = 0; sprintf (drive, "%c:\\", i + 'a'); - GetVolumeInformation (drive, name, sizeof (name), &serno, &maxnamelen, - &flags, fsname, sizeof (fsname)); + + /* + * Report all errors, except if the Volume is ERROR_NOT_READY. + * ERROR_NOT_READY is returned when removeable media drives are empty (CD, floppy, etc.) + */ + if ((0 == GetVolumeInformation (drive, name, sizeof (name), &serno, &maxnamelen, + &flags, fsname, sizeof (fsname))) && (GetLastError() != ERROR_NOT_READY)) + keyeprint("dump_sysinfo(): GetVolumeInformation()"); int dtype = GetDriveType (drive); char drive_type[4] = "unk"; @@ -779,6 +944,8 @@ dump_sysinfo () case DRIVE_RAMDISK: strcpy (drive_type, "ram"); break; + default: + strcpy (drive_type, "unk"); } long capacity_mb = -1; @@ -822,7 +989,8 @@ dump_sysinfo () name); } - FreeLibrary (k32); + if (0 == FreeLibrary (k32)) + keyeprint("dump_sysinfo(): FreeLibrary()"); SetErrorMode (prev_mode); if (givehelp) { @@ -905,13 +1073,6 @@ dump_sysinfo () } int -keyeprint (const char *name) -{ - fprintf (stderr, "cygcheck: %s failed: %lu\n", name, GetLastError ()); - return 1; -} - -int check_keys () { HANDLE h = CreateFileA ("CONIN$", GENERIC_READ | GENERIC_WRITE, @@ -919,17 +1080,17 @@ check_keys () OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (h == INVALID_HANDLE_VALUE || h == NULL) - return keyeprint ("Opening CONIN$"); + return (keyeprint ("check_key(): Opening CONIN$")); DWORD mode; if (!GetConsoleMode (h, &mode)) - keyeprint ("GetConsoleMode"); + keyeprint ("check_key(): GetConsoleMode()"); else { mode &= ~ENABLE_PROCESSED_INPUT; if (!SetConsoleMode (h, mode)) - keyeprint ("GetConsoleMode"); + keyeprint ("check_key(): GetConsoleMode()"); } fputs ("\nThis key check works only in a console window,", stderr); @@ -983,6 +1144,8 @@ check_keys () fputc ('\n', stdout); break; + default: + break; } } while (in.EventType != KEY_EVENT ||