Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:18:43+00:00 2026-06-09T18:18:43+00:00

#include <stdio.h> #include <string.h> #define SRC_BUFF_SIZE 32 #define DST_BUFF_SIZE 8 int tempfn1(char *p) {

  • 0
#include <stdio.h>
#include <string.h>

#define SRC_BUFF_SIZE 32
#define DST_BUFF_SIZE 8

int tempfn1(char *p)
{
    printf("p %p\n", p);
    return 0;
}

int tempfn(char *ip, int size)
{
    char pttt[DST_BUFF_SIZE];
    printf("ip %p\n", ip);
    tempfn1(ip);
    // ERROR - copying more data to a local buffer of 4 bytes

    //memcpy(pttt, ip, size); // This will lead to stack corruption as     
                              // the size exceeds the size of destination

    // IDEALLY the copy should be done with min of size of destination buffer 
    // or source size rather than source size... 
    // anyways dest can hold only the size so it is better to crop the buffer 
    // than to crash due to overflow. 
    // proper call is as follows
    #define MIN(a,b) (((a) < (b)) ? (a) : (b))
    memcpy(pttt, ip, MIN(size, DST_BUFF_SIZE));

    printf("ip %p\n", ip);
    tempfn1(ip);
    return 0;
}

int main()
{
    char ip[SRC_BUFF_SIZE] = {0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 
    0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 
    0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 
    0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2, 0xe2 };
    tempfn(ip, SRC_BUFF_SIZE);
    return 0;
}

This a sample program to avoid stack corruption. Is there any other function to check length of destination as well as the source inorder to avoid stack corruption?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:18:45+00:00Added an answer on June 9, 2026 at 6:18 pm

    The concept which you are pointing to doesn’t apply only to stack corruption. It is applied in general to avoid memory overflow. A memory overflow can lead to corruption of stack when used with stack variable, or heap when used with heap variable. Basically it leads to undefined behavior.

    The best way to avoid this is to:

    1. To program correctly (i.e. with proper logic) as Kerrek SB correctly points out in his comment.
    2. To use functions like strncpy instead of strcpy, strncat instead of strcat etc which will help in applying the same concept of safety which you have tried to apply here.

    The following two links about Secure Coding Guidelines and Practices might be of some help:

    http://www.atsec.com/downloads/pdf/secure-coding-guidelines.pdf
    https://www.securecoding.cert.org/confluence/display/seccode/Top+10+Secure+Coding+Practices

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size;
#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
#include <stdio.h> #include<stdlib.h> #define LIST.H onus; int main () { char *p,*s; printf( LIST.H
#include <stdio.h> #include <string.h> #define MAXLINES 5000 char *lineptr[MAXLINES]; int readlines(char *lineptr[], int nlines);
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
#include<stdio.h> #include<string.h> #define MAX_VAL 100 //Function declaration int input_values(int Z[][k], int j, int k);
Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending
#include <stdio.h> #include <ctype.h> #define STRING_LEN 500 void stripspaces(char, char, char); int main(void) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.