Loose-Info.com
Last Update 2021/09/29
TOP - 各種テスト - gcc - 警告関連のオプション - -Wstringop-overflow

-Wstringop-overflow
strcpyなどの文字列操作関数で、コピー先バッファがオーバーフローすると判断された場合に警告を出力
オプション引数を伴う場合などの詳細の解説は Using the GNU Compiler Collection を参照

テスト概要

オプション無しで実行した際の警告出力例

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。

使用ファイル


sample.c
#include <stdio.h> #include <string.h> int main(void) { char sampbuf[5]; strcpy(sampbuf, "1234"); strncpy(sampbuf, "1234", 5); /* コピー先バッファをオーバーフロー */ strcpy(sampbuf, "12345"); strncpy(sampbuf, "12345", 6); printf("%s\n", sampbuf); return 0; }

動作テスト

オプション無しでコンパイルを実行
$ gcc sample.c sample.c: In function ‘main’: sample.c:12:2: warning: ‘__builtin_memcpy’ writing 6 bytes into a region of size 5 overflows the destination [-Wstringop-overflow=] strcpy(sampbuf, "12345"); ^~~~~~~~~~~~~~~~~~~~~~~~ sample.c:13:2: warning: ‘__builtin_memcpy’ writing 6 bytes into a region of size 5 overflows the destination [-Wstringop-overflow=] strncpy(sampbuf, "12345", 6); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ $ コピー先格納サイズを上回りオーバーフローとなる事に対する警告 デフォルトで有効となるため、オプション無しでも発生