2

I am attempting to resolve the following warning.

warning C26485: Expression 'pNMLink->item.szUrl':
No array to pointer decay. (bounds.3...)

This warning is caused by the following code.

auto pNMLink = reinterpret_cast<PNMLINK>(pNMHDR);
ShellExecuteW(nullptr, L"open", pNMLink->item.szUrl, nullptr, nullptr, SW_SHOWNORMAL);

The extremely limited documentation of the C++ Core Guidelines suggests that the solution is to use a span.

array decay -- use span (from the GSL)

Unfortunately, it gives no information on how to construct that span from the array.

I have tried the following.

gsl::span<wchar_t> url(pNMLink->item.szUrl);

I have tried a number of other options as well. They all result in the same exact warning. What magical incantation do I need to use to avoid the warning?

1
  • 1
    Not sure about span but the way to avoid array decay that I know of is to pass it by reference. For example (&pNMLink->item.szUrl)[L_MAX_URL_LENGTH] or something like that. Commented Feb 16, 2018 at 4:53

1 Answer 1

3

This would appear to be an error in the static analysis tool. How do I know? Because you don't get this warning if you do this:

int arr[10];
gsl::span<int> sp1(arr);

But you do get the warning if you do this:

struct Arr { int arr[10]; };
Arr test;
gsl::span<int> sp2(test.arr);

gsl::span has a constructor that takes array parameters by reference, so when you pass an array directly, there should be no decaying. As shown by sp1, which gives no warning. And there's no C++ reason why fetching the array from a struct would provoke decay when using an automatic variable does not.

So clearly, this is a bug in the analysis tool, a false positive.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.