Recently I was working on an API which took a string as one of the parameters. The code did something like
1 STDAPI FooBarAPI(LPCWSTR pszArg)
2 {
3 bool fOpAllowed = IsSomeOpAllowed(pszArg);
4
5 if (true == fOpAllowed)
6 {
7 DoSomeOp(pszArg);
8 }
9 }
2 {
3 bool fOpAllowed = IsSomeOpAllowed(pszArg);
4
5 if (true == fOpAllowed)
6 {
7 DoSomeOp(pszArg);
8 }
9 }
Now, there was a security issue (or inconsistency depending on what you are doing) lurking in this API. I shouldnt be performing an action based on a decision using the string passed to me. Why? Since there is a window of oppurtunity between 3 & 7 when the caller could change what pszArg points to. Its always recommended to copy pointer data into a local copy before performing any action since that gaurantees you that the data cannot be changed in the middle of your function.