디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

다음 언어는 무슨 언어일까요?

*루비*갤로그로 이동합니다. 2025.02.28 21:20:24
조회 40 추천 0 댓글 0

// This file contains an example that illustrates the use of strings in

// Checked C.  The example is adapted from "The C Programming Language,

// Second Edition", by Brian Kernighan and Dennis Ritchie, p. 69.

// It reads a series of lines and check whether a string occurs in a line.

// If it does, it prints the line.

//

// To compile the file using the Checked C version of clang, on Unix/Mac use

//  clang -o find-pattern find-pattern.c

// On Windows use:

//  clang -o find-pattern.exe find-pattern.c

//

// To run it, create a file with some lines of text in it that contain a

// pattern you wish to match.  Then run:

//    find-pattern {your pattern} < {your file name}

// where you use your pattern and file name in replace of {your pattern}

// and {your file name}.

//

// This file illustrates two important points about using strings in Checked C.

// - Sometimes you need to allocate a fixed size buffer that will be treated

//   as both a null-terminated string and a regular array.  The buffer should

//   be given a null-terminated array type, not a regular array type.

// - You will need to either allocate an extra character, or avoid writes to the

//   last character in the buffer.

// - Take care when using array subscripting on strings.  It is easy to accidentally

//   march beyond the end of the declared bounds.


#include <stdchecked.h>

#include <stdio_checked.h>


// In the original code, this is 1000.  Make this 50 so we can easily test

// exceeding the maximum number of characters allowed in a line.

#define MAXLINE 50


#pragma CHECKED_SCOPE ON


int getline(char line checked[] : count(max), int max);

int strindex(char source nt_checked[], char searchfor nt_checked[]);

void print_string(char str nt_checked[]);


int main(int argc, nt_array_ptr<char> argv checked[] : count(argc)) {

  if (argc < 2) {

    // Th original code used printf.  Calls to variable argument functions

    // aren't allowed in checked scopes, so use separate statements.

    print_string("Usage: ");

    print_string(argv[0]);

    print_string(" pattern\n");

    return -1;

  }

  nt_array_ptr<char> pattern = argv[1];

  // Line is treated is both a null-terminated array and a regular array. 

  // We don't allow array_ptrs to be converted to nt_array_ptrs, but we allow

  // the reverse. For this reason, we make this a null-terminated array.  Add

  // an extra byte so that we have a place for the null terminator and retain

  // the original size (the count of  accessible elements is one less than the

  // dimension of the array).

  char line nt_checked[MAXLINE + 1] = {};

  int found = 0;

  while (getline(line, MAXLINE) > 0)

    if (strindex(line, pattern) >= 0) {

      // Avoid using printf again;

      print_string(line);

      found++;

    }

  return 0;

}


// Get line into s, return length.

int getline(char s checked[] : count(lim), int lim) {

  int c, i;

  // The original code modifies lim, which we are using as the

  // bounds.  Introduce a new variable for counting the available

  // space.

  int available = lim;


  i = 0;

  while (--available > 0 && (c = getchar()) != EOF && c != '\n')

    s[i++] = c;

  if (c == '\n')

    s[i++] = c;

  // We could have made s a nt_checked array.  We'd have to check

  // here to make sure we aren't trying to write to the null terminator.

  s[i] = '\0';

  return i;

}


// Return index of t in s, -1 if none.

int strindex(char source nt_checked[] : count(0),

             char searchfor nt_checked[] : count(0)) {

  int i = 0, k = 0;


  nt_array_ptr<char> s : count(i) = source;

  nt_array_ptr<char> t : count(k) = searchfor;


  for (i = 0; s[i] != '\0'; i++) {

    // The original code was:

    //  for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)

    // The problem with this approach is that on the second iteration, j > i,

    // which will cause a runtime bounds failure for s[j].

    // We just introduce a temporary instead and remove s[j].

    nt_array_ptr<char> tmp_s = s + i;

    for (k = 0; t[k] != '\0' && *tmp_s == t[k]; tmp_s++, k++)

      ;

     if (k > 0 && t[k] == '\0')

       return i;

  }

  return -1;

}


void print_string(char str nt_checked[]) {

  // Fputs has a bounds-safe interface and can be used in checked scopes.

  fputs(str, stdout);

}




추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
등록순정렬 기준선택
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 과음으로 응급실에 가장 많이 갔을 것 같은 스타는? 운영자 25/03/03 - -
2823750 본인은 방금 섹스하고 나옴 [4] hrin(220.120) 02.28 130 0
2823749 120mm 팬이 7개 있는데요.. *루비*갤로그로 이동합니다. 02.28 42 0
2823748 10년안에 ai운영체제 나올거임 ㅇㅇ(211.246) 02.28 37 0
2823747 절망 밖에 없어 자러 간다. 넥도리아(112.170) 02.28 60 0
2823746 K엔터의 자회사 S엔터의 상장에 물린 사람들 ㅇㅇ(110.70) 02.28 47 0
2823745 연애도 못해 몬헌도 못해 섹스도 못해 ㅆㅇㅆ(124.216) 02.28 38 0
2823744 내 생전 몬헌 시리즈 단 한번도 발매당일 못해본적이 없었는데 [3] ㅆㅇㅆ(124.216) 02.28 70 0
2823743 이용하실 인간은 이용한다. 사람은 이용의 가치에 따라 다르다. 나쁜놈들 [1] 넥도리아(112.170) 02.28 33 0
2823742 하이퍼캐쥬얼 개발자 다 필요없다. 돈에 미친 세상 넥도리아(112.170) 02.28 36 0
2823741 글 뭐 써볼까 [3] ㅆㅇㅆ(124.216) 02.28 52 0
2823738 ㅆㅇㅆ의 개발글을 염탐 하고 있다. 그냥 보고 있어. 그래도 될까? [1] 넥도리아(112.170) 02.28 143 0
2823737 흠.. 오늘은 진짜 공부/일 좀 하자 하자 제발 [2] *루비*갤로그로 이동합니다. 02.28 46 0
2823734 내가 리눅스 중 진짜 ㅄ 이라고 보는 것들 ㅎㅎ [8] *루비*갤로그로 이동합니다. 02.28 97 1
2823731 젠투 리눅스가 좋은 이유 *루비*갤로그로 이동합니다. 02.28 39 0
2823729 빵하나 더 먹고 진짜 일하자 *루비*갤로그로 이동합니다. 02.28 33 0
2823727 C++ 쓰면 안 되는 이유 [3] *루비*갤로그로 이동합니다. 02.28 85 0
2823726 42 경산 피시너 하고옴 [2] 프갤러(175.121) 02.28 57 0
2823724 개발자 취업 회사티어질문좀 [4] 프갤러(175.195) 02.28 115 0
2823723 ㅈㄴ 열심히 만들었는데 오버 엔지니어링이면 어캄? [2] 프갤러(221.146) 02.28 49 0
2823722 일하기도 싫고 공부하기도 싫고.. 흠.. *루비*갤로그로 이동합니다. 02.28 35 0
2823721 인지과학조져라 손발이시립디다갤로그로 이동합니다. 02.28 39 0
2823720 젠투리눅스 좋긴좋은데 가끔 역겹게 시간을 쳐먹어서 뒷통수타이밍한방(1.213) 02.28 35 0
2823719 메이플 접겠다고 접긴 했는데 그래서 이제 뭐해야하냐 ㅇㅅㅇ ㅇㅇ(223.33) 02.28 30 0
2823718 나님 금주말엔 이 애니 조면서 좀 쉬어야징 [1] ♥냥덩이♥갤로그로 이동합니다. 02.28 42 0
다음 언어는 무슨 언어일까요? *루비*갤로그로 이동합니다. 02.28 40 0
2823714 아마 C 스펙에 GLIB, APR 같은 API 들어오면 *루비*갤로그로 이동합니다. 02.28 37 0
2823712 C++이 좋은 점은 글쎄요.. ㅎㅎ [7] *루비*갤로그로 이동합니다. 02.28 90 0
2823711 냥람쥐❤+ ♥냥덩이♥갤로그로 이동합니다. 02.28 38 0
2823710 가끔은 C++ 이 무섭기도 하다. 프갤러(59.16) 02.28 60 0
2823708 스타벅스 우드 트레이 어떻게 처분하죠? [7] *루비*갤로그로 이동합니다. 02.28 44 0
2823706 난 여름보다 겨울이 더 좋다 [2] *루비*갤로그로 이동합니다. 02.28 43 0
2823701 옆집 섹스중이네 [5] 프갤러(175.223) 02.28 517 14
2823699 엄마가 삼겹살 구워준다 [12] 멍청한유라ㅋ갤로그로 이동합니다. 02.28 106 0
2823698 FreeBSD는 메모리 소비량 왜이리 적나요? [6] *루비*갤로그로 이동합니다. 02.28 62 0
2823697 C++ vs Ada [1] 프갤러(59.16) 02.28 47 0
2823696 pyoapple 프갤러(39.7) 02.28 35 0
2823695 빨리 여름이 와야 난방 안 틀구 잘 텐댕.. ♥냥덩이♥갤로그로 이동합니다. 02.28 27 0
2823694 creamcandy123 프갤러(39.7) 02.28 51 0
2823692 요즘 프로그래밍언어를 너무 대충짔네 [3] ㅇㅇ(106.102) 02.28 76 0
2823691 Ada 언어 괜찮냐? [6] 딱님갤로그로 이동합니다. 02.28 69 0
2823690 이제 드디어 워드프레스 설치에 들어갑니다. 이름이 그게 뭡니까? *루비*갤로그로 이동합니다. 02.28 34 0
2823689 영화관 왔는데 [1] 발명도둑잡기갤로그로 이동합니다. 02.28 36 0
2823688 알파고도 만드는 데 스포츠 토토는 그게 안되나? ㅋㅋ(211.224) 02.28 33 0
2823686 모모링 볼따구 빵빵하게 만들어주고 싶당❤+ [1] ♥냥덩이♥갤로그로 이동합니다. 02.28 38 0
2823685 애플페이 신한 화요일에 개시!! ♥냥덩이♥갤로그로 이동합니다. 02.28 58 0
2823684 본인 원대한 계획 꿈꾸는중임 [5] 프갤러(221.146) 02.28 61 0
2823682 나님 애널 약속 미루고 그냥 쉬길 잘한듯 갑자기 존나 졸림 ♥냥덩이♥갤로그로 이동합니다. 02.28 32 0
2823681 스타벅스 우드트레이가 사은품으로 왔어요 [6] *루비*갤로그로 이동합니다. 02.28 43 0
2823678 나님 어제 온라인 사전 코테 쳤는데 딱님갤로그로 이동합니다. 02.28 173 0
2823676 나님 기분 ㄱㅆㅅㅌㅊ !! [1] ♥냥덩이♥갤로그로 이동합니다. 02.28 37 0
뉴스 故서희원 유산, 사실상 정리…”구준엽·2자녀 1/3씩 분배” 디시트렌드 03.03
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2