#include<stdlib.h>// this is in the data sectionconstchar*HELLO="hello";// this is in the BSS sectionlongcounter;voidmain(){// this is in the stack memoryintval;// the msg pointer is in the stack memory// the msg content is in the heap memorychar*msg=malloc(120);// msg content is explicitly freed herefree(msg);// the val and msg pointer is implicitly freed here}// the global memory is only destroyed on program exit
typedefstructResponse{intstatus;charmessage[40];}response_t;voidprocess(){response_t*res=malloc(sizeof(response_t));// allocate memory on the heapstructResponseres2;// allocate memory on the stack}
intfoo(intx){intarr[16]={0};returnarr[x];//x is not checked}longfoo(){inta=0;return*(long*)(&a);//a is not long}intfoo(int*p){// if p == NULL, then dereference p will cause a segmentation faultreturn*p+42;}
intdtls1_process_heartbeat(SSL*s){unsignedchar*p=&s->s3->rrec.data[0],*pl;unsignedshorthbtype;unsignedintpayload;unsignedintpadding=16;/* Use minimum padding *//* Read type and payload length first */hbtype=*p++;n2s(p,payload);pl=p;if(s->msg_callback)s->msg_callback(0,s->version,TLS1_RT_HEARTBEAT,&s->s3->rrec.data[0],s->s3->rrec.length,s,s->msg_callback_arg);if(hbtype==TLS1_HB_REQUEST){unsignedchar*buffer,*bp;intr;/* Allocate memory for the response, size is 1 bytes * message type, plus 2 bytes payload length, plus * payload, plus padding */buffer=OPENSSL_malloc(1+2+payload+padding);bp=buffer;/* Enter response type, length and copy payload */*bp++=TLS1_HB_RESPONSE;s2n(payload,bp);memcpy(bp,pl,payload);bp+=payload;/* Random padding */RAND_pseudo_bytes(bp,padding);r=dtls1_write_bytes(s,TLS1_RT_HEARTBEAT,buffer,3+payload+padding);if(r>=0&&s->msg_callback)s->msg_callback(1,s->version,TLS1_RT_HEARTBEAT,buffer,3+payload+padding,s,s->msg_callback_arg);OPENSSL_free(buffer);}return0;}