Sponsored Content
Top Forums Programming Newline in ANSI-C standard functions Post 303020110 by yifangt on Friday 13th of July 2018 01:13:10 PM
Old 07-13-2018
unclear with memset() with strcpy()/strncpy()

Thank you so much for your patience with my basics still, as I feel getting better to understand more.
strncpy doesn't want the size of the origin string: It wants the size of the destination buffer. ...... If you'd used strcpy, you would have been safe.
To see how things are working in the memory in order to understand those garbage left, I got error in the Block Two of following code:***buffer overflow detected ***: ./memset01 terminated
Code:
#include <stdio.h>
#include <string.h>

//More about that some said don't use strcpy() but use strncpy()!!!
int main () {
   char str1[128];
   char str2[256];
   int len1, len2;
   len1=strlen("this is string1");
   len2=strlen("That is test string2");
   printf("Length of str1[128]: %d\n", len1);
   printf("Length of str2[256]: %d\n\n", len2);

   strcpy(str1, "this is string1");
   strcpy(str2, "That is test string2");

   printf("Length of strlen(str1): %lu\n", strlen(str1));
   printf("Length of strlen(str2): %lu\n\n", strlen(str2));

/*Start of block One***********************/
   memset(str1, 'X', strlen(str1)+113); // fills the first 128 bytes of the memory area pointed to by str1 with the constant byte 'X'.
   memset(str2, 'X', strlen(str2)+108); // fills the first 128 bytes of the memory area pointed to by str2 with the constant byte 'X'.

   strncpy(str1, "this is string1", len1);
   strncpy(str2, "That is test string2", len2);
   puts("___Line 1___:");
   puts(str1);
   puts("___Line 2___:");
   puts(str2);
   printf("------------------------------------------------------------------\n");
/*End of block One***********************/

/*Start of block Two***********************/
   memset(str1, 'X', strlen(str1)+113); //fills the first 128 bytes of the memory area pointed to by str1 with the constant byte 'X'.
   memset(str2, 'X', strlen(str2)+108); //fills the first 128 bytes of the memory area pointed to by str2 with the constant byte 'X'.
   strcpy(str1, "this is string1");
   strcpy(str2, "That is test string2");

   puts("___Line 1___:");
   puts(str1);
   puts("___Line 2___:");
   puts(str2);
   printf("------------------------------------------------------------------\n");
/*End of block Two***********************/
   return(0);
}

If I switch the two blocks, there is no buffer overflow for the memset().
What is going on in the memory? Thanks!

Last edited by yifangt; 07-16-2018 at 12:00 PM.. Reason: typos
 

10 More Discussions You Might Find Interesting

1. Programming

Ansi C

Dear All, I have to develope some C functions in Unix for a Magic program. The original MSE code which compiles the attached C program uses a +z option, but the cc compiler don't know this. The complete command in the compiler script is 'cc -c -Aa +z myfile.c'. The warning message is 'The -z... (4 Replies)
Discussion started by: Frankie
4 Replies

2. Programming

ANSI C vs POSIX

can somebody explain about the ANSI C vs POSIX. say i was using open and fopen, i know that open is POSIX, and fopen is ANSI C. i read that that POSIX is a system call and ANSI C is like a standard library function. wouldn't the fopen function has to call on open function anyway to open any kind... (2 Replies)
Discussion started by: bb00y
2 Replies

3. Shell Programming and Scripting

Convert file from Unix - ANSI to PC - ANSI

Hi, I am creating a file in Unix using a shell script. The file is getting created in the Unix - ANSI format. My requirement is to convert it to the PC - ANSI format. Can anyone tell me how to do this? Thanks, Sunil (0 Replies)
Discussion started by: ssmallya
0 Replies

4. Programming

using c++ and c standard I/O functions

Is it not a healthy practice to mix C and C++ standard I/O functions together e.g. string name; // this is a declared instance of the string class in C++ printf("\nPlease enter your name: "); cin >> name; I did something similar in a program Im designing, and used it several... (1 Reply)
Discussion started by: JamesGoh
1 Replies

5. Programming

Standard UNIX functions

Hi everybody, first of all i apologize if my thread's title doesn't make much sense,but i coudn't find a more appropriate name :) Then i apologize about my question,which probably will sound trivial for you :) :) I am working on a program which is being tested in Linux but the final target is... (2 Replies)
Discussion started by: Zipi
2 Replies

6. Shell Programming and Scripting

standard error to standard out question

Hi there how can i get the result of a command to not give me its error. For example, on certain systems the 'zfs' command below is not available, but this is fine becaues I am testing against $? so i dont want to see the message " command not found" Ive tried outputting to /dev/null 2>&1 to no... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

7. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

8. Shell Programming and Scripting

Standard out and standard error

I need to run a cronjob and in the cronjob I execute a script that if there is an error produces standard error so I do /RUNMYSCRIPT 2> mylogfile.log However, if it runs correctly, I don't get a standard error output, I get a standard out output. How do I redirect both standard error and... (2 Replies)
Discussion started by: guessingo
2 Replies

9. Programming

why the implementatoin of Bakery algorithm in ANSI C does not work in ANSI C

I follow the description of wiki (Lamport's bakery algorithm - Wikipedia, the free encyclopedia), then implement that algorithm in C, but it doesn't work, Starving is still here, is the implementation worry? Only print out: Thread ID: 0 START! Thread ID: 0 END! Thread ID: 0 START!... (2 Replies)
Discussion started by: sehang
2 Replies

10. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies
PERROR(3)							 Library functions							 PERROR(3)

NAME
perror - print a system error message SYNOPSIS
#include <stdio.h> void perror(const char *s); #include <errno.h> const char *sys_errlist[]; int sys_nerr; DESCRIPTION
The routine perror() produces a message on the standard error output, describing the last error encountered during a call to a system or library function. First (if s is not NULL and *s is not NUL) the argument string s is printed, followed by a colon and a blank. Then the message and a new-line. To be of most use, the argument string should include the name of the function that incurred the error. The error number is taken from the external variable errno, which is set when errors occur but not cleared when non-erroneous calls are made. The global error list sys_errlist[] indexed by errno can be used to obtain the error message without the newline. The largest message num- ber provided in the table is sys_nerr -1. Be careful when directly accessing this list because new error values may not have been added to sys_errlist[]. When a system call fails, it usually returns -1 and sets the variable errno to a value describing what went wrong. (These values can be found in <errno.h>.) Many library functions do likewise. The function perror() serves to translate this error code into human-readable form. Note that errno is undefined after a successful library call: this call may well change this variable, even though it succeeds, for example because it internally used some other library function that failed. Thus, if a failing call is not immediately followed by a call to perror, the value of errno should be saved. CONFORMING TO
ANSI C, BSD 4.3, POSIX, X/OPEN SEE ALSO
strerror(3) 2001-12-14 PERROR(3)
All times are GMT -4. The time now is 07:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy