1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-08-01 11:26:53 +03:00

ossfuzz: fix picky compiler warnings, make it pass checksrc

- fix compiler warnings.
- make it pass `checksrc`.
- fix shell `set -u` errors.
  Follow-up to 5012442850 #901
- REUSE: tidy up ossfuzz files.

Cherry-picked from #1484
Closes #1526
This commit is contained in:
Viktor Szakats
2024-11-24 18:05:34 +01:00
parent a3aa6b4ca8
commit 96cbe61896
6 changed files with 119 additions and 118 deletions

View File

@ -37,7 +37,6 @@ path = [
"tests/openssh_server/ca_*", "tests/openssh_server/ca_*",
"tests/openssh_server/ssh_*", "tests/openssh_server/ssh_*",
"tests/openssh_server/sshd_config", "tests/openssh_server/sshd_config",
"tests/ossfuzz/*",
"tests/test_read_algos.txt", "tests/test_read_algos.txt",
"vms/libssh2_config.h", "vms/libssh2_config.h",
"vms/libssh2_*.dcl", "vms/libssh2_*.dcl",
@ -51,6 +50,7 @@ path = [
"m4/.gitignore", "m4/.gitignore",
"src/.gitignore", "src/.gitignore",
"tests/.gitignore", "tests/.gitignore",
"tests/ossfuzz/.gitignore",
] ]
SPDX-FileCopyrightText = "The libssh2 project and its contributors." SPDX-FileCopyrightText = "The libssh2 project and its contributors."
SPDX-License-Identifier = "BSD-3-Clause" SPDX-License-Identifier = "BSD-3-Clause"

View File

@ -6,5 +6,5 @@ set -e
cd "$(dirname "$0")/.." cd "$(dirname "$0")/.."
git ls-files "*.[ch]" | xargs -n1 \ git ls-files "*.[ch]" "*.cc" | xargs -n1 \
./ci/checksrc.pl -i4 -m79 -AFOPENMODE -ASNPRINTF -ATYPEDEFSTRUCT ./ci/checksrc.pl -i4 -m79 -AFOPENMODE -ASNPRINTF -ATYPEDEFSTRUCT

View File

@ -1,3 +1,6 @@
# Copyright (C) The libssh2 project and its contributors.
# SPDX-License-Identifier: BSD-3-Clause
AM_CPPFLAGS = -I$(top_builddir)/include AM_CPPFLAGS = -I$(top_builddir)/include
LDADD = $(top_builddir)/src/libssh2.la LDADD = $(top_builddir)/src/libssh2.la

View File

@ -1,4 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright (C) The libssh2 project and its contributors.
# SPDX-License-Identifier: BSD-3-Clause
set -eu set -eu
@ -8,12 +10,12 @@ set -eu
# Save off the current folder as the build root. # Save off the current folder as the build root.
export BUILD_ROOT="$PWD" export BUILD_ROOT="$PWD"
echo "CC: $CC" echo "CC: ${CC:-}"
echo "CXX: $CXX" echo "CXX: ${CXX:-}"
echo "LIB_FUZZING_ENGINE: $LIB_FUZZING_ENGINE" echo "LIB_FUZZING_ENGINE: ${LIB_FUZZING_ENGINE:-}"
echo "CFLAGS: $CFLAGS" echo "CFLAGS: ${CFLAGS:-}"
echo "CXXFLAGS: $CXXFLAGS" echo "CXXFLAGS: ${CXXFLAGS:-}"
echo "OUT: $OUT" echo "OUT: ${OUT:-}"
MAKEFLAGS+="-j$(nproc)" MAKEFLAGS+="-j$(nproc)"
export MAKEFLAGS export MAKEFLAGS

View File

@ -14,12 +14,14 @@
#include "testinput.h" #include "testinput.h"
#define FUZZ_ASSERT(COND) \ #define FUZZ_ASSERT(COND) \
do { \
if(!(COND)) \ if(!(COND)) \
{ \ { \
fprintf(stderr, "Assertion failed: " #COND "\n%s", \ fprintf(stderr, "Assertion failed: " #COND "\n%s", \
strerror(errno)); \ strerror(errno)); \
assert((COND)); \ assert((COND)); \
} } \
} while(0)
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{ {
@ -36,15 +38,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
goto EXIT_LABEL; goto EXIT_LABEL;
} }
// Create a socket pair so data can be sent in. /* Create a socket pair so data can be sent in. */
rc = socketpair(AF_UNIX, SOCK_STREAM, 0, socket_fds); rc = socketpair(AF_UNIX, SOCK_STREAM, 0, socket_fds);
FUZZ_ASSERT(rc == 0); FUZZ_ASSERT(rc == 0);
written = send(socket_fds[1], data, size, 0); written = send(socket_fds[1], data, size, 0);
if(written != size) if(written != (ssize_t)size) {
{ /* Handle whatever error case we're in. */
// Handle whatever error case we're in.
fprintf(stderr, "send() of %zu bytes returned %zu (%d)\n", fprintf(stderr, "send() of %zu bytes returned %zu (%d)\n",
size, size,
written, written,
@ -53,13 +54,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
} }
rc = shutdown(socket_fds[1], SHUT_WR); rc = shutdown(socket_fds[1], SHUT_WR);
if(rc) if(rc) {
{
fprintf(stderr, "socket shutdown failed (%d)\n", rc); fprintf(stderr, "socket shutdown failed (%d)\n", rc);
goto EXIT_LABEL; goto EXIT_LABEL;
} }
// Create a session and start the handshake using the fuzz data passed in. /* Create a session and start the handshake using the fuzz data
passed in. */
session = libssh2_session_init(); session = libssh2_session_init();
if(session) { if(session) {
libssh2_session_set_blocking(session, 1); libssh2_session_set_blocking(session, 1);
@ -72,17 +73,16 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
goto EXIT_LABEL; goto EXIT_LABEL;
} }
// If we get here the handshake actually completed. /* If we get here the handshake actually completed. */
handshake_completed = 1; handshake_completed = 1;
EXIT_LABEL: EXIT_LABEL:
if(session) if(session) {
{ if(handshake_completed) {
if(handshake_completed)
{
libssh2_session_disconnect(session, libssh2_session_disconnect(session,
"Normal Shutdown, Thank you for playing"); "Normal Shutdown, "
"Thank you for playing");
} }
libssh2_session_free(session); libssh2_session_free(session);

View File

@ -16,15 +16,13 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ii; int ii;
for(ii = 1; ii < argc; ii++) for(ii = 1; ii < argc; ii++) {
{
FILE *infile; FILE *infile;
printf("[%s] ", argv[ii]); printf("[%s] ", argv[ii]);
/* Try and open the file. */ /* Try and open the file. */
infile = fopen(argv[ii], "rb"); infile = fopen(argv[ii], "rb");
if(infile) if(infile) {
{
uint8_t *buffer = NULL; uint8_t *buffer = NULL;
size_t buffer_len; size_t buffer_len;
@ -32,15 +30,14 @@ int main(int argc, char **argv)
/* Get the length of the file. */ /* Get the length of the file. */
fseek(infile, 0L, SEEK_END); fseek(infile, 0L, SEEK_END);
buffer_len = ftell(infile); buffer_len = (size_t)ftell(infile);
/* Reset the file indicator to the beginning of the file. */ /* Reset the file indicator to the beginning of the file. */
fseek(infile, 0L, SEEK_SET); fseek(infile, 0L, SEEK_SET);
/* Allocate a buffer for the file contents. */ /* Allocate a buffer for the file contents. */
buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t)); buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t));
if(buffer) if(buffer) {
{
/* Read all the text from the file into the buffer. */ /* Read all the text from the file into the buffer. */
fread(buffer, sizeof(uint8_t), buffer_len, infile); fread(buffer, sizeof(uint8_t), buffer_len, infile);
printf("Read %zu bytes, fuzzing.. ", buffer_len); printf("Read %zu bytes, fuzzing.. ", buffer_len);
@ -54,8 +51,7 @@ int main(int argc, char **argv)
free(buffer); free(buffer);
buffer = NULL; buffer = NULL;
} }
else else {
{
fprintf(stderr, fprintf(stderr,
"[%s] Failed to allocate %zu bytes \n", "[%s] Failed to allocate %zu bytes \n",
argv[ii], argv[ii],
@ -66,9 +62,9 @@ int main(int argc, char **argv)
fclose(infile); fclose(infile);
infile = NULL; infile = NULL;
} }
else else {
{ /* Failed to open the file.
/* Failed to open the file. Maybe wrong name or wrong permissions? */ Maybe wrong name or wrong permissions? */
fprintf(stderr, "[%s] Open failed. \n", argv[ii]); fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
} }