Skip to content

asyncio generic fails when the buffer is larger than the file contents #13719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/io/generic/SDL_asyncio_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static void SynchronousIO(SDL_AsyncIOTask *task)
} else {
const bool writing = (task->type == SDL_ASYNCIO_TASK_WRITE);
task->result_size = (Uint64) (writing ? SDL_WriteIO(io, ptr, size) : SDL_ReadIO(io, ptr, size));
if (task->result_size == task->requested_size) {
if (task->result_size <= task->requested_size) {
task->result = SDL_ASYNCIO_COMPLETE;
} else {
if (writing) {
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ add_library(sdltests_utils OBJECT
)
target_link_libraries(sdltests_utils PRIVATE SDL3::Headers)

file(GLOB RESOURCE_FILES *.bmp *.wav *.csv *.hex moose.dat utf8.txt)
file(GLOB RESOURCE_FILES *.bmp *.wav *.csv *.hex moose.dat utf8.txt small.txt)

option(SDLTEST_TRACKMEM "Run tests with --trackmem" OFF)

Expand Down
1 change: 1 addition & 0 deletions test/small.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
38 changes: 35 additions & 3 deletions test/testasyncio.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ static SDL_Renderer *renderer = NULL;
static SDL_Texture *texture = NULL;
static SDL_AsyncIOQueue *queue = NULL;
static SDLTest_CommonState *state = NULL;
static char READ_BUFFER[4096] = { };

SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
const char *base = NULL;
SDL_AsyncIO *asyncio = NULL;
SDL_AsyncIO *read_asyncio = NULL;
char **bmps = NULL;
int bmpcount = 0;
int i;
Expand Down Expand Up @@ -103,6 +105,24 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
}
}

{
char* path =NULL;
if (SDL_asprintf(&path, "%s%s", base, "small.txt") < 0) {
SDL_free(path);
SDL_Log("Failed!");
return SDL_APP_FAILURE;
}

read_asyncio = SDL_AsyncIOFromFile(path, "r");
SDL_free(path);

/* buffer should be bigger than the file contents */
if (SDL_ReadAsyncIO(read_asyncio, READ_BUFFER, 0, 4096, queue, "small.txt") == false) {
SDL_Log("Failed!");
return SDL_APP_FAILURE;
}
}

SDL_free(bmps);

SDL_Log("Opening asyncio.tmp...");
Expand Down Expand Up @@ -130,7 +150,7 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
return SDLTest_CommonEventMainCallbacks(state, event);
}

static void async_io_task_complete(const SDL_AsyncIOOutcome *outcome)
static SDL_AppResult async_io_task_complete(const SDL_AsyncIOOutcome *outcome)
{
const char *fname = (const char *) outcome->userdata;
const char *resultstr = "[unknown result]";
Expand All @@ -146,7 +166,14 @@ static void async_io_task_complete(const SDL_AsyncIOOutcome *outcome)
SDL_Log("File '%s' async results: %s", fname, resultstr);

if (SDL_strncmp(fname, "asyncio.tmp", 11) == 0) {
return;
return SDL_APP_SUCCESS;
}

if (SDL_strncmp(fname, "small.txt", 9) == 0) {
if (outcome->result != SDL_ASYNCIO_COMPLETE) {
return SDL_APP_FAILURE;
}
return SDL_APP_SUCCESS;
}

if (outcome->result == SDL_ASYNCIO_COMPLETE) {
Expand All @@ -164,13 +191,18 @@ static void async_io_task_complete(const SDL_AsyncIOOutcome *outcome)

SDL_free(outcome->userdata);
SDL_free(outcome->buffer);
return SDL_APP_SUCCESS;
}

SDL_AppResult SDL_AppIterate(void *appstate)
{
SDL_AppResult result;
SDL_AsyncIOOutcome outcome;
if (SDL_GetAsyncIOResult(queue, &outcome)) {
async_io_task_complete(&outcome);
result = async_io_task_complete(&outcome);
if (result != SDL_APP_SUCCESS) {
return result;
}
}

SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
Expand Down
Loading