From b341979adb950ae6abd518df3a170db9e9708797 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 17 Jun 2011 14:39:37 +1000 Subject: util: moved nt_errstr() into common code this brings nt_errstr() into common code, using the new talloc_stackframe_exists() to ensure that we only allocate an error string using talloc_tos() if a talloc stackframe does currently exists. This makes it safe to use in external libraries Pair-Programmed-With: Andrew Bartlett --- libcli/util/nterr.c | 29 ++++++++++++++++++++++------- libcli/util/wscript_build | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'libcli/util') diff --git a/libcli/util/nterr.c b/libcli/util/nterr.c index 1e84e81878..1158fddd76 100644 --- a/libcli/util/nterr.c +++ b/libcli/util/nterr.c @@ -1,7 +1,10 @@ /* * Unix SMB/CIFS implementation. * RPC Pipe client / server routines + * * Copyright (C) Luke Kenneth Casson Leighton 1997-2001. + * Copyright (C) Andrew Bartlett + * Copyright (C) Andrew Tridgell * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -909,15 +912,12 @@ NTSTATUS nt_status_squash(NTSTATUS nt_status) /***************************************************************************** Returns an NT error message. not amazingly helpful, but better than a number. - - This version is const, and so neither allocates memory nor uses a - static variable for unknown errors. *****************************************************************************/ -const char *nt_errstr_const(NTSTATUS nt_code) +const char *nt_errstr(NTSTATUS nt_code) { - static char msg[40]; int idx = 0; + char *result; while (nt_errs[idx].nt_errstr != NULL) { if (NT_STATUS_V(nt_errs[idx].nt_errcode) == @@ -927,7 +927,22 @@ const char *nt_errstr_const(NTSTATUS nt_code) idx++; } - return "unknown NT_STATUS error"; + if (!talloc_stackframe_exists()) { + /* prevent memory leaks from talloc_tos() by using a + * static area. This means the caller will overwrite + * the string with subsequent calls, which can cause + * display of the wrong error. If that happens the + * caller should have a talloc stackframe + */ + static char msg[20]; + snprintf(msg, sizeof(msg), "NT code 0x%08x", NT_STATUS_V(nt_code)); + return msg; + } + + result = talloc_asprintf(talloc_tos(), "NT code 0x%08x", + NT_STATUS_V(nt_code)); + SMB_ASSERT(result != NULL); + return result; } /************************************************************************ @@ -947,5 +962,5 @@ const char *get_friendly_nt_error_msg(NTSTATUS nt_code) /* fall back to NT_STATUS_XXX string */ - return nt_errstr_const(nt_code); + return nt_errstr(nt_code); } diff --git a/libcli/util/wscript_build b/libcli/util/wscript_build index 6a078259db..d87f0ba88e 100644 --- a/libcli/util/wscript_build +++ b/libcli/util/wscript_build @@ -3,6 +3,6 @@ bld.SAMBA_SUBSYSTEM('LIBCLI_ERRORS', source='doserr.c errormap.c nterr.c', - public_deps='talloc' + public_deps='talloc samba-util-common' ) -- cgit