npm

Nihal's Password Manager
git clone git://git.nihaljere.xyz/npm
Log | Files | Refs | README | LICENSE

commit 6d9fcad38179645487da9b2b3316e3c0e25aadd7
parent 0e9795b70afc0a2b4b787ef8e28f4f42f26ff495
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Wed, 12 Jan 2022 22:45:21 -0600

npmc: close socket properly before creation FILE

Diffstat:
Mnpmc.c | 17+++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/npmc.c b/npmc.c @@ -23,14 +23,19 @@ char abspath[PATH_MAX]; int main(int argc, char *argv[]) { - FILE *sockfile; const struct sockaddr_un sockaddr = { .sun_family = AF_UNIX, .sun_path = SOCKPATH }; + if (argc != 2) return 1; + if (realpath(argv[1], abspath) == NULL) { + fprintf(stderr, "failed to get absolute path of %s\n", argv[1]); + goto end; + } + int sock = socket(AF_UNIX, SOCK_STREAM, 0); if (sock == -1) { fprintf(stderr, "failed to create socket: %s\n", strerror(errno)); @@ -39,17 +44,13 @@ main(int argc, char *argv[]) if (connect(sock, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) == -1) { fprintf(stderr, "failed to connect to socket: %s\n", strerror(errno)); - goto closesock; - } - - if (realpath(argv[1], abspath) == NULL) { - fprintf(stderr, "failed to get absolute path of %s\n", argv[1]); - goto closesock; + close(sock); + goto end; } xwrite(sock, abspath, strlen(abspath) + 1); // include terminator - sockfile = fdopen(sock, "rw"); + FILE *sockfile = fdopen(sock, "rw"); if (!sockfile) { perror("failed to open socket as FILE"); close(sock);