0

So I've got a struct called stationInfo, which has a bunch of info, including latitude, longitude, and station ID. I have written a function that will puts reads from a file and stores the values into arrays of structs. Now, I want to move those arrays of structs into another array of structs.

MapMarker mapInfo[t];
int k;
for(k=0; k < MAX_STATIONS; k++){
    mapInfo[k].location.latitude = stationInfo[k].location.latitude;
    mapInfo[k].location.longitude = stationInfo[k].location.longitude;
    char* stationName = getStationName(stationInfo[k].stationID);
    strcpy(mapInfo[k].markerName, stationName);
}

However, this is breaking my program. How can I fix this?

EDIT: As per Paddy's request:

mapMarker Struct:

typedef struct{
GeographicPoint location;
char markerName[100];
char markerText[1000];
int type;
} MapMarker;

GeographicPoint location is split into a latitude and logitude struct.

char* getStationName(int stationID){
if (stationID < 0 || stationID >= MAX_STATIONS || !AllStationNames[stationID])
    return "Unknown";
return AllStationNames[stationID];
} /* getStationName */

And the Array

char *AllStationNames[MAX_STATIONS] = {
[1] = "Ian Stewart Complex/Mt. Douglas High School",
[3] = "Strawberry Vale Elementary School",
...
[197] = "RASC Victoria Centre",
[199] = "Trial Island Lightstation",
[200] = "Longacre",
};
8
  • 1
    Show your MapMarker struct definition. Also, you should show getStationName. Commented Nov 12, 2013 at 3:29
  • There you are. P.S You're my hero Paddy :) Commented Nov 12, 2013 at 3:34
  • 1
    What is the value of t you are using for the VLA declaration of mapInfo? Surely you meant to use MAX_STATIONS. Commented Nov 12, 2013 at 3:36
  • I was going to use MAX_STATIONS, but I thought that t gives me the ammount of stations I truely have, so why declare any more than that. But now that I look at it, its probably just better to have it as MAX_STATIONS. Commented Nov 12, 2013 at 3:39
  • Well, if t can be less than MAX_STATIONS, you will have a buffer overrun. I don't see anything wrong with the strcpy. Using strncpy is safer, but I think the problem is mismatching the array size with the number of elements you are writing to. Of course, you could just loop to t instead of MAX_STATIONS. Commented Nov 12, 2013 at 3:41

1 Answer 1

1

As discussed in comments, you are declaring a VLA (variable-length array) using the variable t as size. That is always less than or equal to MAX_STATIONS. So you have a buffer overrun issue.

MapMarker mapInfo[t];
int k;
for(k=0; k < MAX_STATIONS; k++){
    // Accessing mapInfo[k] when k >= t will have undefined behaviour
}

The simplest solution is to make mapInfo constant-size and loop to t:

MapMarker mapInfo[MAX_STATIONS];
for( k = 0; k < t; k++ ) ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.