2
0

Fix some rewrite bugs.

This commit is contained in:
Maarten Billemont 2017-08-23 00:53:14 -04:00
parent 5ee700c9b9
commit 18eaeec1de
4 changed files with 36 additions and 43 deletions

View File

@ -332,7 +332,7 @@ static bool mpw_marshall_write_json(
if (site->url) if (site->url)
json_object_object_add( json_site_mpw, "url", json_object_new_string( site->url ) ); json_object_object_add( json_site_mpw, "url", json_object_new_string( site->url ) );
mpw_free_strings( &content, &loginContent ); mpw_free_strings( &content, &loginContent, NULL );
} }
mpw_string_pushf( out, "%s\n", json_object_to_json_string_ext( json_file, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED ) ); mpw_string_pushf( out, "%s\n", json_object_to_json_string_ext( json_file, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED ) );

View File

@ -129,6 +129,7 @@ bool __mpw_free_strings(const char **strings, ...) {
va_list args; va_list args;
va_start( args, strings ); va_start( args, strings );
success &= mpw_free_string( strings );
for (const char **string; (string = va_arg( args, const char ** ));) for (const char **string; (string = va_arg( args, const char ** ));)
success &= mpw_free_string( string ); success &= mpw_free_string( string );
va_end( args ); va_end( args );

View File

@ -132,17 +132,17 @@ bool mpw_push_int(
bool __mpw_realloc(const void **buffer, size_t *bufferSize, const size_t deltaSize); bool __mpw_realloc(const void **buffer, size_t *bufferSize, const size_t deltaSize);
/** Free a buffer after zero'ing its contents, then set the reference to NULL. */ /** Free a buffer after zero'ing its contents, then set the reference to NULL. */
#define mpw_free(buffer, bufferSize) \ #define mpw_free(buffer, bufferSize) \
({ typeof(buffer) _b = buffer; const void *__b = *_b; __mpw_free((const void **)_b, bufferSize); }) ({ typeof(buffer) _b = buffer; const void *__b = *_b; __mpw_free( (const void **)_b, bufferSize ); })
bool __mpw_free( bool __mpw_free(
const void **buffer, const size_t bufferSize); const void **buffer, const size_t bufferSize);
/** Free a string after zero'ing its contents, then set the reference to NULL. */ /** Free a string after zero'ing its contents, then set the reference to NULL. */
#define mpw_free_string(string) \ #define mpw_free_string(string) \
({ typeof(string) _s = string; const char *__s = *_s; __mpw_free_string((const char **)_s); }) ({ typeof(string) _s = string; const char *__s = *_s; __mpw_free_string( (const char **)_s ); })
bool __mpw_free_string( bool __mpw_free_string(
const char **string); const char **string);
/** Free strings after zero'ing their contents, then set the references to NULL. Terminate the va_list with NULL. */ /** Free strings after zero'ing their contents, then set the references to NULL. Terminate the va_list with NULL. */
#define mpw_free_strings(strings, ...) \ #define mpw_free_strings(strings, ...) \
({ typeof(strings) _s = strings; const char *__s = *_s; __mpw_free_strings((const char **)strings, __VA_ARGS__); }) ({ typeof(strings) _s = strings; const char *__s = *_s; __mpw_free_strings( (const char **)_s, __VA_ARGS__ ); })
bool __mpw_free_strings( bool __mpw_free_strings(
const char **strings, ...); const char **strings, ...);

View File

@ -271,6 +271,17 @@ int main(int argc, char *const argv[]) {
return EX_USAGE; return EX_USAGE;
} }
} }
MPKeyPurpose keyPurpose = MPKeyPurposeAuthentication;
if (keyPurposeArg) {
keyPurpose = mpw_purposeWithName( keyPurposeArg );
if (ERR == (int)keyPurpose) {
ftl( "Invalid purpose: %s\n", keyPurposeArg );
return EX_USAGE;
}
}
const char *keyContext = NULL;
if (keyContextArg)
keyContext = strdup( keyContextArg );
// Find the user's sites file. // Find the user's sites file.
FILE *sitesFile = NULL; FILE *sitesFile = NULL;
@ -355,52 +366,33 @@ int main(int argc, char *const argv[]) {
// Load the site object. // Load the site object.
for (size_t s = 0; s < user->sites_count; ++s) { for (size_t s = 0; s < user->sites_count; ++s) {
site = &user->sites[s]; site = &user->sites[s];
if (strcmp( siteName, site->name ) != 0) { if (strcmp( siteName, site->name ) == 0)
site = NULL; // Found.
continue; break;
} site = NULL;
break;
} }
if (!site) if (!site)
site = mpw_marshall_site( user, siteName, MPResultTypeDefault, MPCounterValueDefault, user->algorithm ); site = mpw_marshall_site( user, siteName, MPResultTypeDefault, MPCounterValueDefault, user->algorithm );
mpw_free_string( &siteName ); mpw_free_string( &siteName );
// Load the purpose and context / question object. // Load the question object.
MPKeyPurpose keyPurpose = MPKeyPurposeAuthentication; switch (keyPurpose) {
if (keyPurposeArg) { case MPKeyPurposeAuthentication:
keyPurpose = mpw_purposeWithName( keyPurposeArg ); case MPKeyPurposeIdentification:
if (ERR == (int)keyPurpose) { break;
ftl( "Invalid purpose: %s\n", keyPurposeArg ); case MPKeyPurposeRecovery:
mpw_marshal_free( &user ); for (size_t q = 0; q < site->questions_count; ++q) {
mpw_free_string( &sitesPath ); question = &site->questions[q];
return EX_USAGE; if ((!keyContext && !strlen( question->keyword )) ||
} (keyContext && strcmp( keyContext, question->keyword ) != 0))
} // Found.
const char *keyContext = NULL;
if (keyContextArg) {
keyContext = strdup( keyContextArg );
switch (keyPurpose) {
case MPKeyPurposeAuthentication:
// NOTE: keyContext is not persisted.
break;
case MPKeyPurposeIdentification:
// NOTE: keyContext is not persisted.
break;
case MPKeyPurposeRecovery:
for (size_t q = 0; q < site->questions_count; ++q) {
question = &site->questions[q];
if (strcmp( keyContext, question->keyword ) != 0) {
question = NULL;
continue;
}
break; break;
} question = NULL;
break; }
} if (!question)
question = mpw_marshal_question( site, keyContext );
break;
} }
if (!question)
question = mpw_marshal_question( site, keyContext );
// Initialize purpose-specific operation parameters. // Initialize purpose-specific operation parameters.
MPResultType resultType = MPResultTypeDefault; MPResultType resultType = MPResultTypeDefault;