comparison sidlib.c @ 249:1f1e7934ae61

Few improvements to validity checking of STIL file parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 05 Jan 2020 10:17:36 +0200
parents 537420ccd05b
children c98be346c706
comparison
equal deleted inserted replaced
248:a8605886579d 249:1f1e7934ae61
757 int ret = THERR_OK, field = -1; 757 int ret = THERR_OK, field = -1;
758 SIDLibSTILParserCtx ctx; 758 SIDLibSTILParserCtx ctx;
759 char *tmpStr = NULL, *fieldName = NULL; 759 char *tmpStr = NULL, *fieldName = NULL;
760 size_t strPos; 760 size_t strPos;
761 SIDLibSTILNode *entry = NULL; 761 SIDLibSTILNode *entry = NULL;
762 unsigned int subtune; 762 int subtune;
763 763
764 if (fh == NULL || dbh == NULL) 764 if (fh == NULL || dbh == NULL)
765 return THERR_NULLPTR; 765 return THERR_NULLPTR;
766 766
767 if ((tmpStr = th_malloc(SIDLIB_BUFFER_SIZE + 1)) == NULL) 767 if ((tmpStr = th_malloc(SIDLIB_BUFFER_SIZE + 1)) == NULL)
880 880
881 if ((ret = sidlib_stildb_node_new(&entry, tmpStr)) != THERR_OK) 881 if ((ret = sidlib_stildb_node_new(&entry, tmpStr)) != THERR_OK)
882 goto out; 882 goto out;
883 883
884 th_llist_append_node((th_llist_t **) &dbh->nodes, (th_llist_t *) entry); 884 th_llist_append_node((th_llist_t **) &dbh->nodes, (th_llist_t *) entry);
885
886 // Default subtune is 0, for "main tune" information
885 subtune = 0; 887 subtune = 0;
886 888
887 sidlib_stildb_set_parsemode(&ctx, PM_IDLE); 889 sidlib_stildb_set_parsemode(&ctx, PM_IDLE);
888 } 890 }
889 else 891 else
890 { 892 {
891 VADDCH(ctx.ch) 893 VADDCH(ctx.ch)
892 else 894 else
893 { 895 {
894 ret = th_io_error(fh, THERR_INVALID_DATA, 896 ret = th_io_error(fh, THERR_INVALID_DATA,
895 "Entry filename too long on line #%d", 897 "Entry filename too long on line #%d.",
896 fh->line); 898 fh->line);
897 goto out; 899 goto out;
898 } 900 }
899 ctx.ch = -1; 901 ctx.ch = -1;
900 } 902 }
903 case PM_SUBTUNE: 905 case PM_SUBTUNE:
904 if (ctx.ch == ')') 906 if (ctx.ch == ')')
905 { 907 {
906 BOOL neg = FALSE; 908 BOOL neg = FALSE;
907 909
908 // Subtune indicator 910 // Subtune indicator end
909 tmpStr[strPos] = 0; 911 tmpStr[strPos] = 0;
910 912
911 if (!th_get_int(tmpStr, &subtune, &neg) || neg) 913 if (!th_get_int(tmpStr, (unsigned int *) &subtune, &neg) || neg)
912 { 914 {
913 ret = th_io_error(fh, THERR_INVALID_DATA, 915 ret = th_io_error(fh, THERR_INVALID_DATA,
914 "Entry '%s' subtune indicator not a valid integer on line #%d: '%s'", 916 "Entry '%s' subtune indicator not a valid integer on line #%d: '%s'",
915 entry->filename, fh->line, tmpStr); 917 entry->filename, fh->line, tmpStr);
916 goto out; 918 goto out;
919 }
920
921 if (subtune == 0 || subtune > 128)
922 {
923 th_io_error(fh, THERR_INVALID_DATA,
924 "Entry '%s' subtune number %d is invalid on line #%d.",
925 entry->filename, subtune, fh->line);
926
927 subtune = -1;
917 } 928 }
918 929
919 sidlib_stildb_set_parsemode(&ctx, PM_IDLE); 930 sidlib_stildb_set_parsemode(&ctx, PM_IDLE);
920 } 931 }
921 else 932 else
997 if (ctx.lineStart && ctx.linePos < 8) 1008 if (ctx.lineStart && ctx.linePos < 8)
998 { 1009 {
999 // Field done 1010 // Field done
1000 tmpStr[strPos] = 0; 1011 tmpStr[strPos] = 0;
1001 1012
1002 if (field >= 0) 1013 if (field >= 0 && subtune >= 0)
1003 { 1014 {
1004 char *data; 1015 char *data;
1005 1016
1006 // Supported field, add it 1017 // Supported field, add it
1007 if ((ret = sidlib_stildb_entry_realloc(entry, subtune)) != THERR_OK) 1018 if ((ret = sidlib_stildb_entry_realloc(entry, subtune)) != THERR_OK)