Cloud Build no issues but Workbench Local build has warning

photon
workbench
Tags: #<Tag:0x00007f1ca1b847e0> #<Tag:0x00007f1ca1b84678>

#1

Using Workbench.
Did Particle: Environment Reset

I have a project that I can build in the cloud using the Web IDE as well as from Workbench Cloud Build. No issues/warning. However, when I build locally (with out without OS) I see this warning

warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
     char *tempStr = getTagValue(commandParameters, "T");

Now I realize that I need to fix this C++ code but I’m not sure how to. So my questions are:

  1. Why, when I build in the cloud I don’t see this warning.
  2. How do I fix this warning (I’m not a full time C++ programmer)?

The getTagValue method’s signature looks like this

char *getTagValue(char *a_tag_list, char *a_tag)

The code that has the issue is

    char commandParameters[length + 1];
    memcpy(commandParameters, &payload[0], length);
    char *tempStr = getTagValue(commandParameters, "T");

where payload is

byte* payload

Essentially the getTagValue method parses the delimited char* in the form

name1=value1&name2=value2&name3=value3

so when I call getTagValue like so:

char *tempStr = getTagValue(commandParameters, "name2");

I get value2 as the returned value


#2

That doesn’t mean there aren’t any warnings - they are just not displayed when the build succeedes.
If you make the build fail (e.g. by adding a stray { at the end of your code) you will get a error report which will probably also contain the warnings.

The problem the waring informs you about is that you cannot modify a string literal but char* would indicate that the function may do so.
If you use const char* the warning should hopefully go away.


#3

Correct you are @ScruffR (as usual). Thank you.
How do I fix the code so as to not get a warning?


#4

Have you seen the latter part of my post above?
Does this help?

BTW, you also want to terminate the string after you do memcpy(commandParameters, &payload[0], length); before passing the string to your function. Unterminated strings may result in really bad and most annoyingly unexpected behaviour.
Try this

memcpy(commandParameters, payload, length);
commandParameters[length] = '\0';

#5

@ScruffR, yes it did help! Sorry I didn’t see the later part of your earlier message.

For some reason I though the issue was with my tempStr local variable. so before I posted here I had tried to define it as a const char* but that didn’t help. Then in your message you said “string literal” and then got me to look at the “T” being passed in and so I applied a const to the getTagValue method’s second parameter. and that fixed it.

Thank you!

Yes, of course! Don’t know how I missed that. Thank you once again @ScruffR.