1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-31 17:18:04 +03:00

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 <abartlet@samba.org>
This commit is contained in:
Andrew Tridgell 2011-06-17 14:39:37 +10:00 committed by Andrew Bartlett
parent 1233ba7bf3
commit b341979adb
2 changed files with 23 additions and 8 deletions

View File

@ -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);
}

View File

@ -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'
)