#include<iostream>
#include"memchk.h"
using namespace std;
//new를 재정의하는 다양한 기법
int main(){
int *p1 = new int;
int *p2 = new int;
int *p3 = new int;
delete p2;
/*
cout << __FILE__ << endl;
cout << __LINE__ << endl;
cout << __TIME__ << endl;
*/ //표준 C/C++ 에서 지원하는 메크로
return 0; // 다른 헤더에서 메임을 가짜로 바꿨으니.. 리턴0을 써줘야한다
}
====================================memchk.h==============================================
#include<iostream>
using namespace std;
struct MEM{
char file[256];
int line;
void * addr;
};
MEM mem[1000];
int count =0;
void * operator new (size_t sz , char *file , int line){
void *p = malloc(sz);
//할당정보를 배열에 보관한다.
mem[count].addr = p;
mem[count].line = line;
strcpy(mem[count].file, file);
++count;
return p;
}
void operator delete(void *p){
for(int i=0;i<count ; i++){
if(mem[i].addr ==p){
mem[i] = mem[count-1];
free(p);
--count;
break;
}
}
}
int Main();
int main(){
int ret = Main(); //사용자 메인함수는 단순 호출이고 그뒤에 내가 원하는 결과를 붙인다.
if(count == 0)cout << "메모리 누수가 없습니다"<<endl;
else{
cout << count << "개의 메모리 누수가 있습니다."<<endl;
for(int i=0;i<count;i++){
printf("%s(%d):%p\n",mem[i].file, mem[i].line, mem[i].addr);
}
}
return ret;
}
#define main Main //이후에 메인은 메인이 아니다!!! 그 메인의 리턴값 이후에 무언가를 출력시킴
#define new new(__FILE__, __LINE__) //new를 디파인
int *p = new int;
'C,C++ > Android_FrameWork' 카테고리의 다른 글
[SSM 안드로이드 프레임워크 개발 강의]21. 인터페이스와 인터페이스 탄생 배경 (0) | 2012.08.15 |
---|---|
[SSM 안드로이드 프레임워크 개발 강의]20. 접근변경자와 어뎁터 패턴 (0) | 2012.08.15 |
[SSM 안드로이드 프레임워크 개발 강의]18. New 연산자 이야기 (0) | 2012.08.15 |
[SSM 안드로이드 프레임워크 개발 강의]17. 변환 연산자와 변환 생성자. 그리고 활용. (0) | 2012.08.14 |
[SSM 안드로이드 프레임워크 개발 강의]16. STL 과 함수객체 (0) | 2012.08.14 |
That’s pretty slick ;)
I have a feeling that this can be prevented by using basename();
1
<?php
2
if
(isset(
$_GET
[
'm'
])){
3
$file
=
basename
(
$_GET
[
'm'
]);
4
require_once
'$file'
;
5
}
What are your thoughts on that?
If you just use basename the strings going to end up as “resource=index.php”, checking to see if the file exists (using file_exists) is probably a safer method as it will return false for any php://filter files. A quick preg_match couldn’t hurt either…
1
if
(preg_match(
"/^[A-Z0-9]+$/i"
,
$_GET
[
'm'
])) {
2
if
(
file_exists
(
$_GET
[
'm'
])) {
3
require_once
(
$_GET
[
'm'
]);
4
}
5
}
Why not just have a white list array, even the `$_GET['m']` could produce unwanted results, and better to not leave it up to that.
1
$whiteList
=
array
(
'index'
=>
'index.php'
,
'about'
=>
'about.php'
);
2
if
(in_array(
$_GET
[
'm'
],
$whiteList
)) {
3
require_once
(
$whiteList
[
$_GET
[
'm'
]]);
4
}
else
{
5
require_once
(
$whiteList
[
'index'
]);
6
}
This way, you can easily default it, you know the files that will be included and you leave nothing up to chance. And, if you wanted to, you could name the names of the actual files different to prevent direct access.