From: Zhanna Tsitkov Date: Mon, 9 Feb 2009 19:07:09 +0000 (+0000) Subject: Check if mem allocation was successful X-Git-Tag: krb5-1.8-alpha1~679 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=742ed296c962a3082c9456e1fa5882f769299d2d;p=krb5.git Check if mem allocation was successful git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@21925 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/krb5/os/init_os_ctx.c b/src/lib/krb5/os/init_os_ctx.c index 5aded9bee..ffc8d9336 100644 --- a/src/lib/krb5/os/init_os_ctx.c +++ b/src/lib/krb5/os/init_os_ctx.c @@ -1,7 +1,7 @@ /* * lib/krb5/os/init_ctx.c * - * Copyright 1994, 2007, 2008 by the Massachusetts Institute of Technology. + * Copyright 1994, 2007, 2008, 2009 by the Massachusetts Institute of Technology. * All Rights Reserved. * * Export of this software from the United States of America may @@ -30,7 +30,7 @@ #include "k5-int.h" #include "os-proto.h" -#include "prof_int.h" /* XXX for profile_copy, not public yet */ +#include "prof_int.h" /* XXX for profile_copy, not public yet */ #ifdef USE_KIM #include "kim_library_private.h" @@ -45,10 +45,8 @@ get_from_windows_dir( ) { UINT size = GetWindowsDirectory(0, 0); - *pname = malloc(size + 1 + - strlen(DEFAULT_PROFILE_FILENAME) + 1); - if (*pname) - { + *pname = malloc(size + strlen(DEFAULT_PROFILE_FILENAME) + 2); + if (*pname) { GetWindowsDirectory(*pname, size); strcat(*pname, "\\"); strcat(*pname, DEFAULT_PROFILE_FILENAME); @@ -65,8 +63,8 @@ get_from_module_dir( { const DWORD size = 1024; /* fixed buffer */ int found = 0; - char *p; - char *name; + char *p = NULL; + char *name = NULL; struct _stat s; *pname = 0; @@ -91,7 +89,7 @@ get_from_module_dir( if (found) *pname = name; else - if (name) free(name); + free(name); return 0; } @@ -118,8 +116,7 @@ get_from_registry( const char *value_name = "config"; /* a wannabe assertion */ - if (!pbuffer) - { + if (!pbuffer) { /* * We have a programming error! For now, we segfault :) * There is no good mechanism to deal. @@ -128,26 +125,22 @@ get_from_registry( *pbuffer = 0; if ((rc = RegOpenKeyEx(hBaseKey, key_path, 0, KEY_QUERY_VALUE, - &hKey)) != ERROR_SUCCESS) - { + &hKey)) != ERROR_SUCCESS) { /* not a real error */ goto cleanup; } rc = RegQueryValueEx(hKey, value_name, 0, 0, 0, &size); - if ((rc != ERROR_SUCCESS) && (rc != ERROR_MORE_DATA)) - { + if ((rc != ERROR_SUCCESS) && (rc != ERROR_MORE_DATA)) { /* not a real error */ goto cleanup; } *pbuffer = malloc(size); - if (!*pbuffer) - { + if (!*pbuffer) { retval = ENOMEM; goto cleanup; } if ((rc = RegQueryValueEx(hKey, value_name, 0, 0, *pbuffer, &size)) != - ERROR_SUCCESS) - { + ERROR_SUCCESS) { /* * Let's not call it a real error in case it disappears, but * we need to free so that we say we did not find anything. @@ -159,8 +152,7 @@ get_from_registry( cleanup: if (hKey) RegCloseKey(hKey); - if (retval && *pbuffer) - { + if (retval && *pbuffer) { free(*pbuffer); /* Let's say we did not find anything: */ *pbuffer = 0; @@ -179,7 +171,7 @@ free_filespecs(profile_filespec_t *files) return; for (cp = files; *cp; cp++) - free(*cp); + free(*cp); free(files); } @@ -193,35 +185,29 @@ os_get_default_config_files(profile_filespec_t **pfiles, krb5_boolean secure) krb5_error_code retval = 0; char *name = 0; - if (!secure) - { + if (!secure) { char *env = getenv("KRB5_CONFIG"); - if (env) - { + if (env) { name = strdup(env); if (!name) return ENOMEM; } } - if (!name && !secure) - { + if (!name && !secure) { /* HKCU */ retval = get_from_registry(&name, HKEY_CURRENT_USER); if (retval) return retval; } - if (!name) - { + if (!name) { /* HKLM */ retval = get_from_registry(&name, HKEY_LOCAL_MACHINE); if (retval) return retval; } - if (!name && !secure) - { + if (!name && !secure) { /* module dir */ retval = get_from_module_dir(&name); if (retval) return retval; } - if (!name) - { + if (!name) { /* windows dir */ retval = get_from_windows_dir(&name); } @@ -231,6 +217,8 @@ os_get_default_config_files(profile_filespec_t **pfiles, krb5_boolean secure) return KRB5_CONFIG_CANTOPEN; /* should never happen */ files = malloc(2 * sizeof(char *)); + if (!files) + return ENOMEM; files[0] = name; files[1] = 0; #else /* !_WIN32 */ @@ -241,12 +229,13 @@ os_get_default_config_files(profile_filespec_t **pfiles, krb5_boolean secure) #ifdef USE_KIM /* If kim_library_allow_home_directory_access() == FALSE, we are probably - trying to authenticate to a fileserver for the user's homedir. */ + * trying to authenticate to a fileserver for the user's homedir. + */ if (!kim_library_allow_home_directory_access ()) - secure = 1; + secure = 1; #endif if (secure) { - filepath = DEFAULT_SECURE_PROFILE_PATH; + filepath = DEFAULT_SECURE_PROFILE_PATH; } else { filepath = getenv("KRB5_CONFIG"); if (!filepath) filepath = DEFAULT_PROFILE_PATH; @@ -264,8 +253,7 @@ os_get_default_config_files(profile_filespec_t **pfiles, krb5_boolean secure) return ENOMEM; /* measure, copy, and skip each one */ - for(s = filepath, i=0; (t = strchr(s, ':')) || (t=s+strlen(s)); s=t+1, i++) - { + for(s = filepath, i=0; (t = strchr(s, ':')) || (t=s+strlen(s)); s=t+1, i++) { ent_len = t-s; files[i] = (char*) malloc(ent_len + 1); if (files[i] == 0) { @@ -291,26 +279,26 @@ os_get_default_config_files(profile_filespec_t **pfiles, krb5_boolean secure) static krb5_error_code add_kdc_config_file(profile_filespec_t **pfiles) { - char *file; - size_t count; + char *file = NULL; + size_t count = 0; profile_filespec_t *newfiles; file = getenv(KDC_PROFILE_ENV); if (file == NULL) - file = DEFAULT_KDC_PROFILE; + file = DEFAULT_KDC_PROFILE; for (count = 0; (*pfiles)[count]; count++) - ; + ; count += 2; newfiles = malloc(count * sizeof(*newfiles)); if (newfiles == NULL) - return ENOMEM; + return ENOMEM; memcpy(newfiles + 1, *pfiles, (count-1) * sizeof(*newfiles)); newfiles[0] = strdup(file); if (newfiles[0] == NULL) { - int e = ENOMEM; - free(newfiles); - return e; + int e = ENOMEM; + free(newfiles); + return e; } free(*pfiles); *pfiles = newfiles; @@ -325,7 +313,7 @@ add_kdc_config_file(profile_filespec_t **pfiles) static krb5_error_code os_init_paths(krb5_context ctx, krb5_boolean kdc) { - krb5_error_code retval = 0; + krb5_error_code retval = 0; profile_filespec_t *files = 0; krb5_boolean secure = ctx->profile_secure; @@ -336,11 +324,11 @@ os_init_paths(krb5_context ctx, krb5_boolean kdc) retval = os_get_default_config_files(&files, secure); if (retval == 0 && kdc) - retval = add_kdc_config_file(&files); + retval = add_kdc_config_file(&files); if (!retval) { - retval = profile_init((const_profile_filespec_t *) files, - &ctx->profile); + retval = profile_init((const_profile_filespec_t *) files, + &ctx->profile); #ifdef KRB5_DNS_LOOKUP /* if none of the filenames can be opened use an empty profile */ @@ -374,66 +362,66 @@ os_init_paths(krb5_context ctx, krb5_boolean kdc) krb5_error_code krb5_os_init_context(krb5_context ctx, krb5_boolean kdc) { - krb5_os_context os_ctx; - krb5_error_code retval = 0; + krb5_os_context os_ctx; + krb5_error_code retval = 0; #ifdef _WIN32 WORD wVersionRequested; WSADATA wsaData; #endif /* _WIN32 */ - os_ctx = &ctx->os_context; - os_ctx->magic = KV5M_OS_CONTEXT; - os_ctx->time_offset = 0; - os_ctx->usec_offset = 0; - os_ctx->os_flags = 0; - os_ctx->default_ccname = 0; - - ctx->vtbl = 0; - PLUGIN_DIR_INIT(&ctx->libkrb5_plugins); - PLUGIN_DIR_INIT(&ctx->preauth_plugins); - ctx->preauth_context = NULL; - - retval = os_init_paths(ctx, kdc); - /* - * If there's an error in the profile, return an error. Just - * ignoring the error is a Bad Thing (tm). - */ + os_ctx = &ctx->os_context; + os_ctx->magic = KV5M_OS_CONTEXT; + os_ctx->time_offset = 0; + os_ctx->usec_offset = 0; + os_ctx->os_flags = 0; + os_ctx->default_ccname = 0; + + ctx->vtbl = 0; + PLUGIN_DIR_INIT(&ctx->libkrb5_plugins); + PLUGIN_DIR_INIT(&ctx->preauth_plugins); + ctx->preauth_context = NULL; + + retval = os_init_paths(ctx, kdc); + /* + * If there's an error in the profile, return an error. Just + * ignoring the error is a Bad Thing (tm). + */ - if (!retval) { - krb5_cc_set_default_name(ctx, NULL); + if (!retval) { + krb5_cc_set_default_name(ctx, NULL); #ifdef _WIN32 - /* We initialize winsock to version 1.1 but - * we do not care if we succeed or fail. - */ - wVersionRequested = 0x0101; - WSAStartup (wVersionRequested, &wsaData); + /* We initialize winsock to version 1.1 but + * we do not care if we succeed or fail. + */ + wVersionRequested = 0x0101; + WSAStartup (wVersionRequested, &wsaData); #endif /* _WIN32 */ - } - return retval; + } + return retval; } krb5_error_code KRB5_CALLCONV krb5_get_profile (krb5_context ctx, profile_t *profile) { return profile_copy (ctx->profile, profile); -} +} krb5_error_code krb5_set_config_files(krb5_context ctx, const char **filenames) { - krb5_error_code retval; - profile_t profile; - - retval = profile_init(filenames, &profile); - if (retval) - return retval; - - if (ctx->profile) - profile_release(ctx->profile); - ctx->profile = profile; - - return 0; + krb5_error_code retval = 0; + profile_t profile; + + retval = profile_init(filenames, &profile); + if (retval) + return retval; + + if (ctx->profile) + profile_release(ctx->profile); + ctx->profile = profile; + + return 0; } krb5_error_code KRB5_CALLCONV @@ -454,50 +442,50 @@ krb5_free_config_files(char **filenames) krb5_error_code krb5_secure_config_files(krb5_context ctx) { - /* Obsolete interface; always return an error. - - This function should be removed next time a major version - number change happens. */ - krb5_error_code retval; - - if (ctx->profile) { - profile_release(ctx->profile); - ctx->profile = 0; - } - - ctx->profile_secure = TRUE; - retval = os_init_paths(ctx, FALSE); - if (retval) - return retval; - - return KRB5_OBSOLETE_FN; + /* Obsolete interface; always return an error. + * This function should be removed next time a major version + * number change happens. + */ + krb5_error_code retval = 0; + + if (ctx->profile) { + profile_release(ctx->profile); + ctx->profile = 0; + } + + ctx->profile_secure = TRUE; + retval = os_init_paths(ctx, FALSE); + if (retval) + return retval; + + return KRB5_OBSOLETE_FN; } void krb5_os_free_context(krb5_context ctx) { - krb5_os_context os_ctx; + krb5_os_context os_ctx; - os_ctx = &ctx->os_context; - - if (os_ctx->default_ccname) { - free(os_ctx->default_ccname); - os_ctx->default_ccname = 0; - } + os_ctx = &ctx->os_context; + + if (os_ctx->default_ccname) { + free(os_ctx->default_ccname); + os_ctx->default_ccname = 0; + } - os_ctx->magic = 0; + os_ctx->magic = 0; - if (ctx->profile) { - profile_release(ctx->profile); - ctx->profile = 0; - } + if (ctx->profile) { + profile_release(ctx->profile); + ctx->profile = 0; + } - if (ctx->preauth_context) { - krb5_free_preauth_context(ctx); - ctx->preauth_context = NULL; - } - krb5int_close_plugin_dirs (&ctx->preauth_plugins); - krb5int_close_plugin_dirs (&ctx->libkrb5_plugins); + if (ctx->preauth_context) { + krb5_free_preauth_context(ctx); + ctx->preauth_context = NULL; + } + krb5int_close_plugin_dirs (&ctx->preauth_plugins); + krb5int_close_plugin_dirs (&ctx->libkrb5_plugins); #ifdef _WIN32 WSACleanup();