Skip to content

Commit

Permalink
Reset errno to zero before calling functions (#1256)
Browse files Browse the repository at this point in the history
関数を呼び出した後にerrnoの値をチェックする箇所では関数を呼び出す前に
errnoを0にリセットします。

参考文献
- https://linuxjm.osdn.jp/html/LDP_man-pages/man3/errno.3.html
- https://www.jpcert.or.jp/sc-rules/c-err30-c.html
  • Loading branch information
ma8ma committed Sep 23, 2023
1 parent 92362a3 commit 307d911
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/iomonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void IOMonitor::init_connection()
int mkfifo_status = -1;

do_makefifo:
errno = 0;
mkfifo_status = mkfifo( m_fifo_file.c_str(), O_RDWR | S_IRUSR | S_IWUSR );

// FIFO作成でエラーになった( 基本的に既にメインプロセスがある )
Expand All @@ -73,6 +74,7 @@ void IOMonitor::init_connection()
if( errno == EEXIST )
{
// FIFOを書き込み専用モードでオープン( ノンブロック )
errno = 0;
if( ( m_fifo_fd = open( m_fifo_file.c_str(), O_WRONLY | O_NONBLOCK ) ) == -1 )
{
// 反対側が既にオープンされていない( 異常終了などでメインプロセスがない )
Expand Down
2 changes: 2 additions & 0 deletions src/jdlib/jdiconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Iconv::Iconv( const Encoding to, const Encoding from, const bool broken_sjis_be_
void Iconv::open_by_alternative_names( const char* to_str, const char* from_str )
{
// "MS932"で失敗したら"CP932"で試してみる
errno = 0;
if( m_enc_to == Encoding::sjis ) m_cd = g_iconv_open( "CP932", from_str );
else if( m_enc_from == Encoding::sjis ) m_cd = g_iconv_open( to_str, "CP932" );

Expand Down Expand Up @@ -150,6 +151,7 @@ std::string& Iconv::convert( char* str_in, std::size_t size_in, std::string& out
std::size_t byte_left_in = buf_in_end - buf_in_tmp;
std::size_t byte_left_out = buf_out_end - buf_out_tmp;

errno = 0;
const int ret = g_iconv( m_cd, &buf_in_tmp, &byte_left_in, &buf_out_tmp, &byte_left_out );

#ifdef _DEBUG
Expand Down
11 changes: 9 additions & 2 deletions src/jdlib/jdsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ bool Socket::connect( const std::string& hostname, const std::string& port, cons
}

// connect peer
errno = 0;
err = ::connect( m_soc, ainf->ai_addr, ainf->ai_addrlen );
if( err ) {
// ノンブロックでまだ接続中
Expand Down Expand Up @@ -375,19 +376,24 @@ int Socket::write( const char* buf, const std::size_t bufsize )
return kSocket_ERR;
}

int err;
#ifdef MSG_NOSIGNAL
errno = 0;
tmpsize = ::send( m_soc, buf + bufsize - send_size, send_size, MSG_NOSIGNAL );
err = errno;
#else
// SolarisにはMSG_NOSIGNALが無いのでSIGPIPEをIGNOREする (FreeBSD4.11Rにもなかった)
signal( SIGPIPE, SIG_IGN ); /* シグナルを無視する */
errno = 0;
tmpsize = ::send( m_soc, buf + bufsize - send_size, send_size, 0 );
err = errno;
signal( SIGPIPE, SIG_DFL ); /* 念のため戻す */
#endif // MSG_NOSIGNAL

if( tmpsize == 0
|| ( tmpsize < 0 && !( errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR ) ) )
|| ( tmpsize < 0 && !( err == EAGAIN || err == EWOULDBLOCK || err == EINTR ) ) )
{
m_errmsg = std::string{ "send failed: errno=" } + std::strerror( errno );
m_errmsg = std::string{ "send failed: errno=" } + std::strerror( err );
return kSocket_ERR;
}
}
Expand Down Expand Up @@ -432,6 +438,7 @@ int Socket::read( char* buf, const std::size_t bufsize )
return kSocket_ERR;
}

errno = 0;
ret = ::recv( m_soc, buf, bufsize, 0 );
if( ret < 0 && !( errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR ) ) {

Expand Down
1 change: 1 addition & 0 deletions src/jdlib/jdsocketopenssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ void Socket::tls_close()
{
int ret;

errno = 0;
while( ( ret = SSL_shutdown( m_tls ) ) != 1 ) {
WaitFor want_read = WaitFor::recv;

Expand Down

0 comments on commit 307d911

Please sign in to comment.