LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2020, 03:11 PM   #1
maschelsea
Member
 
Registered: Dec 2016
Distribution: Slackware 64bit 14.2
Posts: 468

Rep: Reputation: Disabled
listing12.10 in Sam's book gives unexpected results


I got hacked around the end of last year. That is why I'm asking this question here. Ever since then, I've been using the users command every time I log into the root account, just to make sure I'm the only one here. I've been working through a programming book, Sam's Teach Yourself C++ For Linux In 21 Days. I read a chapter one day, and over the course of the next week or so I enter the listings. Listing12.10 gave me an output I didn't expect. Can anyone here tell me why this output is so...WRONG?
Code:
michael@caitlyn 12 $ cat listing12.10.cpp
//Listing 12.10 - strcpy, strncpy, strlen, and strcat
//2020-09-03
#include <iostream>
#include <string.h>
int main()
{
   char String1[] = "No man is an island";
   char String2[] = "";
   char String3[] = "";
   std::cout << "String1:  " << String1 << std::endl;
   strcpy(String2, String1);
   std::cout << "String2:  " << String2 << std::endl;
   strncpy(String3, String1, 5); //not entire string
   String3[5] = '\0'; //you need a null terminator
   std::cout << "String3 after strcpy:  " << String3 << std::endl;
   std::cout << "String1 is " << strlen(String1) << " bytes long.\nString2 is " << strlen(String2) 
      << " bytes long.\nString 3 is " << strlen(String3) << " bytes long." << std::endl;
   strcat(String3, String1);
   std::cout << "String3 after strcat:  " << String3 << std::endl;
   std::cout << "String1 is still " << strlen(String1) << " bytes long.\nString2 is still " << strlen(String2) 
      << " bytes long\nString 3 is now " << strlen(String3) << " bytes long.\n";
   return 0;
}

michael@caitlyn 12 $
Here is the output:
Code:
michael@caitlyn 12 $ ./listing12.10
String1:  No man is an island
String2:  No man is an island
String3 after strcpy:  o man
String1 is 3 bytes long.
String2 is 4 bytes long.
String 3 is 5 bytes long.
String3 after strcat:  o manman
String1 is still 6 bytes long.
String2 is still 7 bytes long
String 3 is now 8 bytes long.
And here is the expected output (from the book):
Code:
String1:  No man is an island
String2:  No man is an island
String3 after strncpy:  No ma
String1 is 19 bytes long, String2 is 19 bytes long, and String3 is 5 bytes long.
String3 after strcat:  No maNo man is an island
String1 is still 19 bytes long,
  String25 is still 19 bytes long,
  and String3 is now 24 bytes long.
If it is a Linux virus, (I've heard that Linux is virus-free,) can anyone tell me how to find it, ID it, and get rid of it? Otherwise, why is my program giving me such erroneous output? Thank you in advance to anyone who can help me solve this.

If it matters, this is the line I used to compile the code:
Code:
michael@caitlyn 12 $ g++ -Wall listing12.10.cpp -o listing12.10
 
Old 09-06-2020, 04:14 PM   #2
LuckyCyborg
Senior Member
 
Registered: Mar 2010
Posts: 3,500

Rep: Reputation: 3308Reputation: 3308Reputation: 3308Reputation: 3308Reputation: 3308Reputation: 3308Reputation: 3308Reputation: 3308Reputation: 3308Reputation: 3308Reputation: 3308
AND, which is the relation of your C/C++ adventures with Slackware, so this thread to be worth to be put in this particular Slackware forum?
 
Old 09-06-2020, 05:58 PM   #3
maschelsea
Member
 
Registered: Dec 2016
Distribution: Slackware 64bit 14.2
Posts: 468

Original Poster
Rep: Reputation: Disabled
14.2 64-bit
 
Old 09-07-2020, 09:52 AM   #4
elcore
Senior Member
 
Registered: Sep 2014
Distribution: Slackware
Posts: 1,753

Rep: Reputation: Disabled
Quote:
Originally Posted by maschelsea View Post
I got hacked around the end of last year. That is why I'm asking this question here.
We could probably tell you "how" that happened if there were some sort of logs.
But if you want to know the why and the who, I don't think anyone here will help you with that.

Quote:
Originally Posted by maschelsea View Post
Can anyone here tell me why this output is so...WRONG?
Someone in programming forum, but if you go there remember to ask "how" because "why" is much more difficult to answer.
 
Old 09-07-2020, 10:40 AM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
No mystery here: obvious buffer overflow on the String2[] and String3[] arrays which are allocated on the stack with a single byte each.

Check you typed the listing from the book correctly. I suspect you missed out the array sizes.

Last edited by GazL; 09-07-2020 at 02:41 PM.
 
Old 09-07-2020, 02:54 PM   #6
maschelsea
Member
 
Registered: Dec 2016
Distribution: Slackware 64bit 14.2
Posts: 468

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by GazL View Post
No mystery here: obvious buffer overflow on the String2[] and String3[] arrays which are allocated on the stack with a single byte each.

Check you typed the listing from the book correctly. I suspect you missed out the array sizes.
You're right! Those two arrays should have had [80] instead of []. Thank you for discovering my mistake. This one is SOLVED.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Try_TLS NO gives unexpected results ghughes5669 Linux - Server 1 08-14-2015 06:33 PM
LXer: Serious Sam HD & Serious Sam HD: The Second Encounter Will Come To Linux LXer Syndicated Linux News 0 04-05-2014 09:25 PM
[SOLVED] sed loop gives unexpected results jgombos Programming 13 09-10-2011 12:33 AM
find -mtime gives unexpected results amchargue Linux - Newbie 3 08-12-2010 04:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration