Skip to content

Commit fbae75e

Browse files
committed
NET_ResolveHostname: Trim whitespace off strings as a courtesy.
Fixes #148.
1 parent f5b384c commit fbae75e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/SDL_net.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,27 @@ void NET_Quit(void)
492492
#endif
493493
}
494494

495+
static void trim_whitespace(char *str)
496+
{
497+
char *ptr;
498+
for (ptr = str; *ptr; ptr++) {
499+
if (!SDL_isspace(*ptr)) {
500+
break;
501+
}
502+
}
503+
504+
if (ptr > str) {
505+
SDL_memmove(str, ptr, SDL_strlen(ptr) + 1);
506+
}
507+
508+
for (ptr = str + (SDL_strlen(str) - 1); ptr >= str; ptr--) {
509+
if (!SDL_isspace(*ptr)) {
510+
break;
511+
}
512+
*ptr = '\0';
513+
}
514+
}
515+
495516
NET_Address *NET_ResolveHostname(const char *host)
496517
{
497518
NET_Address *addr = SDL_calloc(1, sizeof (NET_Address));
@@ -505,6 +526,9 @@ NET_Address *NET_ResolveHostname(const char *host)
505526
return NULL;
506527
}
507528

529+
// remove whitespace around name, just in case. https://github.com/libsdl-org/SDL_net/issues/148
530+
trim_whitespace(addr->hostname);
531+
508532
SDL_SetAtomicInt(&addr->refcount, 2); // one for creation, one for the resolver thread to unref when done.
509533

510534
SDL_LockMutex(resolver_lock);

0 commit comments

Comments
 (0)