Why puts function doesn't work with input char from socket in C++? -


this code server running login manager, log file malicious access , print out result of wrong login. chars user , pass come user input using socket.

if ((memcmp(user, "admin", strlen("admin")) == 0)) {     /*code... */ } else {     char msg[600];     strcpy (msg,"login error ");     strcat (msg,"user: ");     strcat (msg,user);     strcat (msg," password: ");     strcat (msg,pass);     strcat (msg," from: ");     strcat (msg, client_ip);     puts (msg);     logfile->write(msg);     return false; } 

well, problem output both on output console , in logfile.

like this:

login error user: lol   password: asd  :��ܔ��p{w� from: 127.0.0.1 

why there strange asci chars? how can avoid new line since come user input socket?

as multiple people have commented about, snippet of code contains nothing c++ specific, i'm answering if working in plain c.

i'm guessing, since use memcmp above, input strings not null terminated. strcat keep on appending chars whatever pointer wanders until runs '\0'. you'll need add null terminator if want use user or password c-style string, or else use strncat , pass length.

also, beware of overrunning msg. might have better luck using snprintf format message, since accepts maximum output string length.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -