LoginSignup
3
2

C Secure Coding Rules(8) 5.18. Failing to close files or free dynamic memory when they are no longer needed [fileclose]

Last updated at Posted at 2018-04-03

ISO/IEC TS 17961:2013

Information Technology — Programming languages, their environments and system software interfaces — C Secure Coding Rules
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1624.pdf

この文書は、ISO/IEC JTC1 SC22 WG14の作業文書(Working Draft)です。
公式のISO/IEC TS 17961:2013原本ではありません。

技術内容を検討し、ISO/IEC JTC1 SC22 WG14にフィードバックするために用いるものです。

ISO/IEC TS 17961:2013 C Secure Coding Rules(1)一覧

https://qiita.com/kaizen_nagoya/items/54e056195c4f11b850a1
一つの規則で複数回のコンパイルが必要な場合、別記事にしています。

作業予定

規則の例(断片等)をコンパイル、実行する予定です。
1: コンパイルエラーが出ないようにする。
 一覧のaccfree.cがこの段階です。
2: 実行時エラーが出ないようにする。
 一覧のptrcomp.cがこの段階です。
3: 意味のある出力が出るようにする。
 検討中。
現状では、変な代入、奇異な操作が頻出します。
コンパイルエラーが出ないようにするなるべく短い記述で済まそうという趣旨で、他意はありません。
よりよい記述に変更する予定です。

現在利用中のコンパイラ

Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.4.0 on macOS 10.13.3
または
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
on macOS 10.12.6

gcc-7 (Homebrew GCC 7.3.0_1) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.

gccは順次追記中です。その後、Visual Studioを予定しています。

環境(Environment)

hosted Environment, macOS 10.13.3 or 10.12.9

コンパイル用shell script

C版(clangとgcc)とC++版(clang++とg++)
https://qiita.com/kaizen_nagoya/items/74220c0577a512c2d7da

5.18. Failing to close files or free dynamic memory when they are no longer needed [fileclose]

EXAMPLE 1 In this noncompliant example, a diagnostic is required because the resource allocated by the call to fopen is not closed.

fileclose.c
// ISO/IEC JTC 1/SC 22/WG 14 N 1624 Date: 2012-06-26 ISO/IEC TS 17961, p.23
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1624.pdf
/// lines are added by Dr. Kiyoshi Ogawa, 2018
/// Compiled on 
///  Clang(LLVM) clang version 6.0.0 (tags/RELEASE_600/final) 
///  GCC(GNU) gcc-7 (Homebrew GCC 7.3.0_1) 7.3.0
/// hosted Environment macOS 10.13.3 or 10.12.9
//5.18. Failing to close files or free dynamic memory when they are no longer needed [fileclose]
//EXAMPLE 1 In this noncompliant example, a diagnostic is required because the resource allocated by the call to fopen is not closed.

#include <stdio.h> // for printf
#include <stdlib.h> // for EXIT_SUCCESS
int f(void) {
  const char *filename = "secure.dat";
  FILE *f = fopen(filename, "r"); // diagnostic required
  if (f == NULL) {
    /* ... */
    printf("file is not open\n");
  }
  /* ... */
  return 0;
}
int main(void) {
  printf("%d \n",f());
  return EXIT_SUCCESS;
}
$./gcc7ts.sh fileclose
$ clang fileclose.c
file is not open0 

$ gcc-7 fileclose.c
file is not open0 

EXAMPLE 2 In this noncompliant example, a diagnostic is required because the resource allocated by the call to malloc is not freed.

fileclose2.c
// ISO/IEC JTC 1/SC 22/WG 14 N 1624 Date: 2012-06-26 ISO/IEC TS 17961, p.23
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1624.pdf
/// lines are added by Dr. Kiyoshi Ogawa, 2018
/// Compiled on 
///  Clang(LLVM) clang version 6.0.0 (tags/RELEASE_600/final) 
///  GCC(GNU) gcc-7 (Homebrew GCC 7.3.0_1) 7.3.0
/// hosted Environment macOS 10.13.3 or 10.12.9
//5.18. Failing to close files or free dynamic memory when they are no longer needed [fileclose]
//EXAMPLE 2 In this noncompliant example, a diagnostic is required because the resource allocated by the call to malloc is not freed.

#include <stdio.h> // for printf
#include <stdlib.h> // for EXIT_SUCCESS

int f(void) {
  char *text_buffer = (char *)malloc(BUFSIZ); // diagnostic required
  if (text_buffer == NULL) {
    return -1;
  }
  return 0;
}
int main(void) {
  printf("%d \n",f());
  return EXIT_SUCCESS;
}
$./gcc7ts.sh fileclose2
$ clang fileclose2.c
0 

$ gcc-7 fileclose2.c
0 

参考文献

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)
https://qiita.com/kaizen_nagoya/items/74220c0577a512c2d7da
C言語(C++)に対する誤解、曲解、無理解、爽快。
https://qiita.com/kaizen_nagoya/items/3f3992c9722c1cee2e3a
MISRA C まとめ #include
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9
どうやって MISRA C Example Suiteをコンパイルするか
https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00
[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。
https://qiita.com/kaizen_nagoya/items/5f4b155030259497c4de

文書履歴

ver. 0.10 初稿 20180404
ver. 0.11 gcc-7,Example節項目追記、修正前後記録 20180408
ver. 0.12 ありがとう追記 20230413

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2