scanf가 오류가 뜨는데 이유 좀 알려 주세요.

ysh1987의 이미지

학생 수를 입력 받은 후 학생 수 대로 점수와 순위 배열을 동적할당 하는 성적처리 프로그램을 짰는데 7줄 scanf에서 안전하지 않은 변수라고 오류가 뜹니다.
어딜 고쳐야 되는지 알려주세요.
#include
#include
void main() {
int i, j, input, stu;
int *score, *rank;
printf("성적처리 할 학생수를 입력 해주세요 :");
scanf("%d", &stu);
score = (int *)malloc(sizeof(int)*stu);
rank = (int *)malloc(sizeof(int)*stu);

for (i = 0; i < stu; i++){
printf("%d 학생의 점수를 입력해주세요: ");
scanf("%d", &input);

score[i] = input;
}
input = stu;
for (i = 0; i < stu; i++){
for (j = 0; j < stu; j++){
if (score[i] >= score[j])
--input;
rank[i] = input;
}
}
printf("번호/t 점수/t 순위/n");
for (i = 0; i < stu; i++){
printf("%d/t %d/t %d/n", i + 1, score[i], rank[i]);
}
free(score);
free(rank);
}

mirheekl의 이미지


제가 보기엔 malloc에 문제의 소지가 있네요. stu가 충분히 클 경우 integer overflow가 발생할 소지가 있습니다. 아마도 그걸 지적하는 에러가 발생하지 않았을까 합니다.

--

qkrwncks593의 이미지

integer overflow 가능성을 컴파일러가 잡아준것을겁니다.

소스를 봤는데 위 소스로 발생할수 있는 취약점은

malloc() 함수의 할당받는 메모리양을 제어할수있고 sizeof(int) * stu 결과값으로 malloc() 을 호출하기 때문에

만약 stu 를 큰값으로 설정하면 integer overflow 가 나서 malloc() 에 5 나 30 이런값으로 넘겨줄수가 있습니다.

그로인해 for() 루프에서는 stu 가 높은값으로 설정되고 할당된 메모리는 매우 작은값이기 때문에

for() 문을 통해 할당된 메모리밖까지 쓰기가 가능해서 이로인해 힙 오버플로우가 발생합니다.

두번째 malloc() 역시 취약점이 존재합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.