0

同じ構成 (fifo ファイル) でアプリケーションの 2 つのインスタンスを実行しないようにする関数を記述します。

if( !filename_specified ) {
    char *fifofile = get_fifo_filename( config_get_uid( ct ) );
    int fifofd;

    if( !fifofile ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
        return 0;
    }

    /* negative_return_fn: Function "open(fifofile, 2049)" returns a negative number */
    fifofd = open( fifofile, O_WRONLY | O_NONBLOCK );
    if( fifofd >= 0 ) {
        lfprintf( stderr, _("Cannot run two instances of application with the same configuration.\n") );
        close( fifofd );
        return 0;
    }
    /* negative_returns: "fifofd" is passed to a parameter that cannot be negative */
    close( fifofd );
}

私のコードはこの警告を受け取ります:

正の値を期待する関数の引数として負の値が使用されました。

4

1 に答える 1

2

open()負の値が返された場合、失敗しました。失敗した場合open()、返された値は有効なファイル記述子ではありません。ファイルは開かれていないため、編集する必要はありません。close()

fifofdにのみ渡すことでclose()、これを修正し0ますopen()

if (0 <= fifofd)
{
  if (-1 == close(fifofd))
  {
    perror("close() failed");
  }
}
于 2013-11-07T07:44:57.950 に答える