简体   繁体   中英

Best way to eliminate deprecated conversion from string constant to ‘char*’"

I have came across a situation as follows and I am getting following warning from the compiler.

    Main.cpp:14:22: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
   Login(USER,PASSWORD);

Here are the codes Passwords.h

#define USER "user"
#define PASSWORD "pass"

Main.cpp

#include <iostream>
#include "Passwords.h"

using namespace std;

void Login(char* username,char* password)
{
  cout << "UserName is " << username <<endl;
  cout << "Password is " << password <<endl;
}

int main()
{
  Login(USER,PASSWORD);
  return 0;
}

One thing I decided to do was to introduce a global variables in the Passwords.h file. But I really like to know what is the best practice to solve this issue.

I hope no one will mark this as duplicate as same question is asked. I need to eliminate this warning in legitimate way and looking for a best practice as many answers for the same problem gave some hacks to turn off compiler warnings and some casting solutions.

Update

The Login function is actually a virtual function so the parameter datatypes cannot be changed from char * to const char* . I used the specific code segment for simplicity.

But I really like to know what is the best practice to solve this issue.

The best practice is to use pointers/references to const when no modification is made to the pointed object/array. You should make the following change to the parameters:

void Login(const char* username, const char* password)

Conversion from string literal to const char* is well-formed and not deprecated.


the parameter datatypes cannot be changed from char * to const char*

Your requirement excludes the best practice shown above. Given this restriction, you should not pass a string literal to the function. The options left are workarounds. The workaround that I would suggest is to create a local copy of the literal, and pass that instead:

char user[] = USER;
char pass[] = PASSWORD;
Login(user,pass);

PS. Since C++11 the conversion from string literal to char* is not only deprecated, but ill-formed instead. A compiler conforming to the current standard may refuse to compile the program.

PPS. As pointed out in the comments, storing a password within the executable as plain text (which is the way string literals are stored) is dubious from security perspective.

Given that you can't change the function signature to something more appropriate, you're stuck. Bad code forces more bad code.

In this case you need to copy the strings to something that isn't const .

int main()
{
  char user[] = USER;
  char password[] = PASSWORD;
  Login(user,password);
  return 0;
}

Just do

void Login(const char* username, const char* password)

While you are on it, replace define with constexpr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM