Skip to content
Merged
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
25 changes: 7 additions & 18 deletions lib/tpm2_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ctype.h>
#include <dlfcn.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -1010,28 +1011,16 @@ bool tpm2_calq_qname(TPM2B_NAME *pqname,
}

bool tpm2_safe_read_from_stdin(int length, char *data) {
int rc;

char *buf = malloc(length);
char *read_data = malloc(length);

if (buf == fgets(buf, length, stdin)) {
rc = sscanf(buf, "%s", read_data);
if (rc != 1) {
free(buf);
free(read_data);
return false;
}
}
else {
free(buf);
free(read_data);
/* Read line from stdin; at most length-1 bytes + null-termination */
if (!fgets(data, length, stdin)) {
return false;
}

strcpy(data, read_data);
free(buf);
free(read_data);
/* Delete newline character */
size_t end = strcspn(data, "\r\n");
data[end] = '\0';

return true;
}

Expand Down
Loading