Commit 46426ab9 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

a safe variant of vlistInqVarName() [patch 8/14 from Nathanael]

parent 6ce92915
......@@ -486,6 +486,9 @@ void vlistDefVarName(int vlistID, int varID, const char *name);
/* vlistInqVarName: Get the name of a Variable */
void vlistInqVarName(int vlistID, int varID, char *name);
/* vlistCopyVarName: Safe and convenient version of vlistInqVarName */
char* vlistCopyVarName(int vlistId, int varId);
/* vlistDefVarStdname: Define the standard name of a Variable */
void vlistDefVarStdname(int vlistID, int varID, const char *stdname);
......
......@@ -150,6 +150,7 @@ vlist_t;
vlist_t *vlist_to_pointer(int vlistID);
void vlistCheckVarID(const char *caller, int vlistID, int varID);
const char *vlistInqVarNamePtr(int vlistID, int varID);
const char *vlistInqVarLongnamePtr(int vlistID, int varID);
const char *vlistInqVarStdnamePtr(int vlistID, int varID);
......
......@@ -14,6 +14,7 @@
#include "namespace.h"
#include "serialize.h"
#include "error.h"
#include "proprietarySystemWorkarounds.h"
static
void vlistvarInitEntry(int vlistID, int varID)
......@@ -128,7 +129,6 @@ int vlistvarNewEntry(int vlistID)
return (varID);
}
static
void vlistCheckVarID(const char *caller, int vlistID, int varID)
{
vlist_t *vlistptr = vlist_to_pointer(vlistID);
......@@ -501,6 +501,49 @@ void vlistInqVarName(int vlistID, int varID, char *name)
return;
}
/*
@Function vlistCopyVarName
@Tatle Get the name of a Variable in a safe way
@Prototype char* vlistCopyVarName(int vlistId, int varId)
@Parameter
@Item vlistID Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
@Item varID Variable identifier.
@Return A pointer to a malloc'ed string. Must be cleaned up with free().
@Description
This is the buffer overflow immune version of vlistInqVarName().
The memory for the returned string is allocated to fit the string via malloc().
@EndFunction
*/
char* vlistCopyVarName(int vlistId, int varId)
{
vlist_t* vlistptr = vlist_to_pointer(vlistId);
vlistCheckVarID(__func__, vlistId, varId);
//If a name is set in the variable description, use that.
const char* name = vlistptr->vars[varId].name;
if(name) return myStrDup(name);
//Otherwise we check if we should use the table of parameter descriptions.
int param = vlistptr->vars[varId].param;
int discipline, category, number;
cdiDecodeParam(param, &number, &category, &discipline);
if(discipline == 255)
{
int tableId = vlistptr->vars[varId].tableID;
if(( name = tableInqParNamePtr(tableId, number) )) return myStrDup(name);
//No luck, fall back to outputting a name of the format "var<num>".
return myAsprintf("var%d", number);
}
//Finally, we fall back to outputting a name of the format "param<num>.<cat>.<dis>".
return myAsprintf("param%d.%d.%d", number, category, discipline);
}
/*
@Function vlistInqVarLongname
@Title Get the longname of a Variable
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment